我在 BitmapFactory 中遇到异常。我已经阅读了其他问题,唯一的解决方案是加载我的图像的缩小版本,但在我的情况下,我需要使用捏缩放系统以全分辨率显示它们(图像是包含文本的传单) . 300ko 的图像只有 466x700px,它们显示如下:
Viewpager 类:
@Override
public View instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(container.getContext());
PhotoViewAttacher attacher = new PhotoViewAttacher(imageView);
try {
Drawable toRecycle = imageView.getDrawable();
if (toRecycle != null) {
((BitmapDrawable) imageView.getDrawable()).getBitmap().recycle();
}
imageView.setImageDrawable(getCat()[position]);
} catch (IOException e) {
e.printStackTrace();
}
attacher.update();
container.addView(imageView);
return imageView;
}
和 getCat() :
public static Drawable[] getTheme_amiante_s_carreleur(Context ctx) throws IOException {
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, 2, 1);
Drawable[] theme_amiante_s_carreleur = {
Drawable.createFromStream(expansionFile.getInputStream("amiante_carreleur_s_1.png"), "src"),
Drawable.createFromStream(expansionFile.getInputStream("amiante_carreleur_s_2.png"), "src"),
Drawable.createFromStream(expansionFile.getInputStream("amiante_carreleur_s_3.png"), "src"),
Drawable.createFromStream(expansionFile.getInputStream("amiante_carreleur_s_4.png"), "src"),
Drawable.createFromStream(expansionFile.getInputStream("amiante_carreleur_s_5.png"), "src"), };
return theme_amiante_s_carreleur;
}
它在我的 Nexus 4 上运行良好,但在 Galaxy S2 或 LG Optimus Black 上,由于内存不足错误,系统迅速强制关闭:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
at com.ctai.iris.ListeImages.getTheme_formation_ce_obligatoires(ListeImages.java:1163)
at com.ctai.iris.ImageViewLow.getCat(ImageViewLow.java:186)
at com.ctai.iris.ImageViewLow$SamplePagerAdapter.instantiateItem(ImageViewLow.java:77)
at com.ctai.iris.ImageViewLow$SamplePagerAdapter.instantiateItem(ImageViewLow.java:1)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
at android.support.v4.view.ViewPager.populate(ViewPager.java:992)
at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:237)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:665)
at dalvik.system.NativeStart.main(Native Method)
知道如何处理这个问题吗?