位图的大小超过旧设备VM预算 [英] bitmap size exceeds VM budget on old devices
问题描述
我收到了BitmapFactory异常。我读过的其他问题,唯一的解决办法是我载入图像的scaledown版本,但对我来说,我需要用一个掐显示它们在全分辨率缩放系统(图像传单,包括文字) 。这些图像仅466x700px为300ko,它们显示是这样的:
I'm getting an Exception in the BitmapFactory. I've read on the other questions that the only solution is to load a scaledown version of my images, but in my case, I need to display them in full resolution with a pinch to zoom system (the images are flyers that include text). The images are only 466x700px for 300ko, they are displayed like this :
Viewpager类:
Viewpager class :
@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():
And the 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擎天柱黑色,系统将迅速强制关闭因内存不足的错误:
It works fine on my Nexus 4, but on a Galaxy S2 or a LG Optimus Black, the system quickly force close due to a Out Of Memory error :
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)
任何想法如何,我可以处理这个问题?
Any idea how I can handle this problem ?
推荐答案
回收位图时不使用。 COM preSS位图,以减少内存使用情况。看看ATH这个链接。 http://developer.android.com/training/displaying-bitmaps/load -bitmap.html 。
Recycle bitmaps when not in use. Compress the bitmap to reduce memory usage. Have a look ath this link. http://developer.android.com/training/displaying-bitmaps/load-bitmap.html.
使用MAT分析仪来检查多少内存使用位图。
Use a MAT Analyzer to check how much memory is used by bitmap.
检查链接。内存管理,以及如何谈判,以使用MAT查找内存泄漏。 http://www.youtube.com/watch?v=_CruQY55HOk 。
Check the link. Talks about memory management and how to use MAT to find memory leaks. http://www.youtube.com/watch?v=_CruQY55HOk.
另外删除静态的可绘。我不知道,但不妨一试。
Also remove the static for Drawables. I am not sure, but give it a try.
这篇关于位图的大小超过旧设备VM预算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!