[英]Android png images big in memory
I have an application in android that has an imageflipper
.我在 android 中有一个应用程序,它有一个imageflipper
。 Problem is, after about 8 images loaded to memory, I get an out of memory error.问题是,在将大约 8 个图像加载到内存后,出现内存不足错误。
Well, I tried to do dynamic image loading, so that if the user flips 2 images, I'll load next 2 to memory and delete 2 first ones.好吧,我尝试进行动态图像加载,这样如果用户翻转 2 张图像,我会将接下来的 2 个加载到内存中并删除 2 个第一个。 It kind of works, but it is ugly and I have trouble when user flips images back( imageflipper.showprevious()
).它有点工作,但它很丑,当用户翻转图像时我有麻烦( imageflipper.showprevious()
)。
I can't really shift all images and place new images to the beginning.我无法真正移动所有图像并将新图像放在开头。
My question is:我的问题是:
Is there a better way to do this kind of stuff?有没有更好的方法来做这种事情? Resizing images didn't really help.调整图像大小并没有真正帮助。
I used the below code snippet to over the memory related issues, We can over come this problem by the help of isRecycled()
method.我使用下面的代码片段来解决与内存相关的问题,我们可以通过isRecycled()
方法来解决这个问题。 Add this code with yours, here finalImage
is my BitmapDrawable
and R.id.image_viewer
is my imageview, you can change it yours将此代码添加到您的代码中,这里finalImage
是我的BitmapDrawable
和R.id.image_viewer
是我的图像R.id.image_viewer
,您可以更改它
@Override
protected void onDestroy() {
finalImage.setCallback(null);
if (!((BitmapDrawable) finalImage).getBitmap().isRecycled()) {
((BitmapDrawable) finalImage).getBitmap().recycle();
}
finalImage = null;
unbindDrawables(findViewById(R.id.image_viewer));
Runtime.getRuntime().gc();
// scaledBitmap.recycle();
System.gc();
super.onDestroy();
}
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
Use BitmapFactory.Options使用BitmapFactory.Options
BitmapFactory.Options opts = new BitmapFactory.Options();
opt.inSampleSize = 2;
//this will decrease bitmap size,
// but also affect quality of the image,
//so just play with this value to spot the good one;
Bitmap b = BitmapFactory.decodeFile(fileName, opts);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.