2016-02-12 71 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 –