我将图像存储在可绘制文件夹中,其大小为 1mb,分辨率为 992x9000,我的代码:
ImageView jpgView = (ImageView) findViewById(R.id.surahShow);
Uri path = Uri.parse("android.resource://com.android.ta/" + R.drawable.myimage);
jpgView.setImageURI(path);
应用程序运行并强制关闭
这是我的 LogCat:
10-28 10:55:07.321: E/AndroidRuntime(596): FATAL EXCEPTION: main
10-28 10:55:07.321: E/AndroidRuntime(596): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
10-28 10:55:07.321: E/AndroidRuntime(596): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:450)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:326)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:349)
10-28 10:55:07.321: E/AndroidRuntime(596): at com.android.juzamma.SingleListSurah.decodeSampleBitmapFromResource(SingleListSurah.java:290)
10-28 10:55:07.321: E/AndroidRuntime(596): at com.android.juzamma.SingleListSurah.onCreate(SingleListSurah.java:199)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.os.Looper.loop(Looper.java:123)
10-28 10:55:07.321: E/AndroidRuntime(596): at android.app.ActivityThread.main(ActivityThread.java:3647)
10-28 10:55:07.321: E/AndroidRuntime(596): at java.lang.reflect.Method.invokeNative(Native Method)
10-28 10:55:07.321: E/AndroidRuntime(596): at java.lang.reflect.Method.invoke(Method.java:507)
10-28 10:55:07.321: E/AndroidRuntime(596): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-28 10:55:07.321: E/AndroidRuntime(596): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-28 10:55:07.321: E/AndroidRuntime(596): at dalvik.system.NativeStart.main(Native Method)
然后我尝试更改我的代码:
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight){
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 2;
if (height/2 < reqHeight || width/2 < reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
public static Bitmap decodeSampleBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
jpgView.setImageBitmap(decodeSampleBitmapFromResource(getResources(), R.drawable.myimage, 100, 100));
但仍然强制关闭
但问题是,我无法用我使用的代码调整他们的代码
我在 android 2.3.1 上做
有人可以帮忙吗