2013-03-06 1 views
0

내가 메모리 오류 벗어나 려하고있다 : 아래 - BitmapFactory.decodeStream

내 코드입니다 :

public static Bitmap decodeSampledBitmapFromResource(InputStream inputStream,int reqWidth, int reqHeight) { 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      byte[] buffer = new byte[1024]; 
      int len; 
      try { 
      while ((len = inputStream.read(buffer)) > -1) { 
       baos.write(buffer, 0, len); 
      } 
      baos.flush(); 
      InputStream is1 = new ByteArrayInputStream(baos.toByteArray()); 
      InputStream is2 = new ByteArrayInputStream(baos.toByteArray()); 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inDither=false;  
     options.inPurgeable=true;     
     options.inInputShareable=true; 
     options.inJustDecodeBounds = true; 
     // BitmapFactory.decodeResource(res, resId, options); 
     BitmapFactory.decodeStream(is1,null,options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 

     return BitmapFactory.decodeStream(is2,null,options); // error at this line 
      } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      return null; 
     } 
    } 

나는이 라인

BitmapFactory.decodeStream(is2,null,options); 

I에서 오류가 발생하고 이 오류가 발생합니다 Out of memory on a 3250016-byte allocation.

나는 많은 게시물을 보았지만 여전히 soluti를 찾을 수 없습니다. 이 경우에.

+0

어떤 오류가 발생합니까? LogCat 또는 StackTrace를 붙여주십시오. –

+0

먼저 메모리에 스트림을로드하는 이유는 무엇입니까? 서버 또는 로컬 파일에서 이미지를 가져 오는 위치부터? – Triode

+0

나는 데이터라는 폴더가있다. 나는 입력 스트림을 반환하는 클래스 로더를 사용하여 imges를 얻고 있는데, 나는이 메소드에 전달하고있다. – Goofy

답변

2

이 기능을 정적으로 설정하지 마십시오. 정적 멤버에 메모리 문제가 있습니다.

초 BitmapFactory.decodeStream (is1, null, options)을 사용하고 있지 않습니다. 어디서나 코드에서 제거하십시오.

또한이 코드를 사용하여 sdcard 경로에서 비트 맵을 가져 와서 사용자의 요구에 맞게 쉽게 수정할 수 있으므로 유사한 상황을 처리하는 데 사용하므로 결코 실패하지 않습니다.

public Bitmap getImage(String path) throws IOException 
    { 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options);   
     int srcWidth = options.outWidth; 
     int srcHeight = options.outHeight; 
     int[] newWH = new int[2]; 
     newWH[0] = srcWidth/2; 
     newWH[1] = (newWH[0]*srcHeight)/srcWidth; 

     int inSampleSize = 1; 
     while(srcWidth/2 >= newWH[0]){ 
      srcWidth /= 2; 
      srcHeight /= 2; 
      inSampleSize *= 2; 
     } 

     //  float desiredScale = (float) newWH[0]/srcWidth; 
     // Decode with inSampleSize 
     options.inJustDecodeBounds = false; 
     options.inDither = false; 
     options.inSampleSize = inSampleSize; 
     options.inScaled = false; 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     Bitmap sampledSrcBitmap = BitmapFactory.decodeFile(path,options); 
     ExifInterface exif = new ExifInterface(path); 
     String s=exif.getAttribute(ExifInterface.TAG_ORIENTATION); 
     System.out.println("Orientation>>>>>>>>>>>>>>>>>>>>"+s); 
     Matrix matrix = new Matrix(); 
     float rotation = rotationForImage(Add_View_Images_Activity.this, Uri.fromFile(new File(path))); 
     if (rotation != 0f) { 
      matrix.preRotate(rotation); 
     } 
     int newh = (w * sampledSrcBitmap.getHeight()) /sampledSrcBitmap.getWidth(); 
     Bitmap r=Bitmap.createScaledBitmap(sampledSrcBitmap, w, newh, true); 
     Bitmap resizedBitmap = Bitmap.createBitmap(
       r, 0, 0, w, newh, matrix, true); 

     return resizedBitmap; 
    } 
+0

안녕하세요. developer.android.com/training/displaying-bitmaps/load-bitmap.html 그 방법을 사용하고, 또한 내가 sdcard에서 경로가 없어 내가 전달해야하므로 입력 스트림이있다 – Goofy

+0

무엇 승입니까? 여기 – Goofy

+0

거기 있습니까? – Goofy

관련 문제