2016-02-12 46 views
1

時同時執行此代碼我收到了內存異常,內存不足的錯誤加載位圖

Back.setImageBitmap(decodeSampledBitmapFromUri(myUri, 50, 50)); 

我用下面的代碼來控制這個錯誤,並能正常工作,但對於更大的圖片此錯誤仍然存在。

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height > reqHeight || width > reqWidth) { 
     if (width > height) { 
      inSampleSize = Math.round((float)height/(float)reqHeight); 
     } else { 
      inSampleSize = Math.round((float)width/(float)reqWidth); 
     } 
    } 
    return inSampleSize; 
} 

public Bitmap decodeSampledBitmapFromUri(Uri uri,int reqWidth, int reqHeight) throws FileNotFoundException { 


    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); 

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

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options); 
} 

完全錯誤的位置:

02-12 14:14:51.888: E/AndroidRuntime(6560): FATAL EXCEPTION: main 
02-12 14:14:51.888: E/AndroidRuntime(6560): java.lang.OutOfMemoryError 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.cut.BlurIt.decodeSampledBitmapFromUri(BlurIt.java:938) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.cut.BlurIt.onCreate(BlurIt.java:223) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.Activity.performCreate(Activity.java:5133) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.ActivityThread.access$600(ActivityThread.java:150) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.os.Looper.loop(Looper.java:213) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at android.app.ActivityThread.main(ActivityThread.java:5225) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at java.lang.reflect.Method.invoke(Method.java:525) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
02-12 14:14:51.888: E/AndroidRuntime(6560):  at dalvik.system.NativeStart.main(Native Method) 

行938:

BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); 

路223:

Back.setImageBitmap(decodeSampledBitmapFromUri(myUri, 10, 10)); 

更新:

我用的這個

InputStream in = null; 

    in = getContentResolver().openInputStream(uri); 
    BitmapFactory.decodeStream(in, null, options); 

,而不是這一行

BitmapFactory.decodeStream(getContentResolver().openInputStream(uri)); 

和它的工作原理和負載圖像,而不內存不足的問題,但我不知道爲什麼它的返回位圖不是正是我所說的。我叫(100,100)返回(172,171),實際大小是(5508,5472)。

+0

你認爲你不需要告訴INT reqWidth,詮釋reqHeight的值並返回inSampleSize; ? – greenapps

+0

'內存不足錯誤發生。'有更多信息可用。你認爲你不必發佈? – greenapps

+0

您必須使用reqHeight&reqWidth的較小值。此外它更好地使用谷歌的代碼:http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html#load-bitmap – X3Btel

回答

2

您可以使用Glide或Picasso庫將位圖加載到ImageView。

Glide.with(context).load(urlOrUri).into(imageViewToLoad); 
+0

的功能,但是爲什麼當我嘗試繪製它時會得到空指針異常。 baseBitmap =((BitmapDrawable)Back.getDrawable())。getBitmap(); 我使用Glide設置圖像。 –

+1

你進入車庫問你的車有什麼問題,但你買了一輛新車。這是一個答案嗎? – greenapps

+0

@greenapps什麼?車庫?是的,這個答案有效。但我有一些問題相同http://stackoverflow.com/questions/34252258/getdrawable-gives-null-object-while-trying-to-get-bitmap-from-imageview –