![](/img/trans.png)
[英]java.lang.outofmemoryerror bitmap size exceeds vm budget on bitmap
[英]ViewPager with ImageView gives “java.lang.OutOfMemoryError: bitmap size exceeds VM budget”
我做了一个ViewPager
以显示图像。 当我前进某些页面时,我得到一个java.lang.OutOfMemoryError: bitmap size exceeds VM budget
错误。
关于此问题还有更多问题,但我没有找到解决方案( BitMap.Recycle
, System.gc()
等)。 如果您有任何建议或解决方案,请告诉我!
PNG是628KB,478KB,587KB,132KB,139KB,149KB,585KB(崩溃)。
如果您还有其他解决方案(滚动类似页面的图像),请告诉我!
我的代码:
package nl.ipear.vngmagazine;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Parcelable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class ShowMagazine2 extends FragmentActivity {
private ViewPager myPager;
private MyPagerAdapter myPagerAdapter;
private static int NUM_PAGES = 15;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.showmagazine);
myPager = (ViewPager)findViewById(R.id.viewpager1);
myPagerAdapter = new MyPagerAdapter();
myPager.setAdapter(myPagerAdapter);
return;
}
private class MyPagerAdapter extends PagerAdapter{
@Override
public int getCount() {
return NUM_PAGES;
}
@Override
public Object instantiateItem(View collection, int position) {
String location = Environment.getExternalStorageDirectory() + "/MYData/" + "2012-02/";
// Inflate and create the view
LayoutInflater layoutInflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.magazinepageview, null);
ImageView imageView = (ImageView) view.findViewById(R.id.magazinePageImage);
String fileName = String.format("%s%s%02d%s", location, "2012-02_Page_", position + 1, ".png");
Log.v("PNG", fileName);
imageView.setImageBitmap(BitmapFactory.decodeFile(fileName));
((ViewPager) collection).addView(view,0);
return view;
}
@Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view==((View)object);
}
@Override
public void finishUpdate(View arg0) {}
@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {}
@Override
public Parcelable saveState() {
return null;
}
@Override
public void startUpdate(View arg0) {}
}
}
在这里找到答案: Android:java.lang.OutOfMemoryError:无法分配20970152可用字节和2MB的23970828字节分配,直到OOM
只需在AndroidManifest内的Application标签中添加android:hardwareAccelerated="false"
和android:largeHeap="true"
:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:hardwareAccelerated="false"
android:largeHeap="true">
看来您的ViewPager正在加载在位图中,但在滚动浏览时未释放任何内容。
我建议您限制当前可见页面两边可用的页面数量,这将使系统能够清除其他页面中的位图。
确保销毁“活动/片段”以解决OOM问题时,您正在回收位图。
是的,跟随Mimminito。 我建议2页,因为3页的来回渲染速度很快。
现在,如果您需要无延迟的图像并且图像已在Internet上。 确保下载它并将其放入internalStorageDevice内,然后重新使用(如果存在)。
对于内存泄漏问题,我的最高答案可能是错误的。 这个新答案可能是内存泄漏的原因,原因是图像的高度和宽度。
private byte[] resizeImage( byte[] input ) {
if ( input == null ) {
return null;
}
Bitmap bitmapOrg = BitmapFactory.decodeByteArray(input, 0, input.length);
if ( bitmapOrg == null ) {
return null;
}
int height = bitmapOrg.getHeight();
int width = bitmapOrg.getWidth();
int newHeight = 250;
float scaleHeight = ((float) newHeight) / height;
// creates matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleHeight, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
bitmapOrg.recycle();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
resizedBitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
resizedBitmap.recycle();
return bos.toByteArray();
}
此顶部代码用于重新缩放位图,“ newHeight”是赋予位图的新高度。
现在,如果这不起作用,我现在100%确定OP必须使用Android内存可以处理的更多视图来重载ViewPager。 解决方案是使用ViewFlipper并使用我上面所述的答案。
尝试缩小您的位图,它确实解决了问题,这是您的操作方法。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile( filename, options );
options.inJustDecodeBounds = false;
options.inSampleSize = 2;
bitmap = BitmapFactory.decodeFile( filename, options );
if ( bitmap != null && exact ) {
bitmap = Bitmap.createScaledBitmap( bitmap, width, height, false );
}
将样本大小调整为任意大小,例如4、8等
在oncreate()中调用System.gc()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.