应用程序在Nexus 10上崩溃,但在Galaxy Tab 3上没有崩溃



我现在正在开发一个安卓应用程序。我包含很多图像。当我在银河选项卡 3 上测试应用程序时,一切正常。在Nexus 10上,由于内存不足错误,应用程序经常崩溃:

Caused by: java.lang.OutOfMemoryError

怎么可能,因为 nexus 10 的内存翻了一番?

我正在使用此类加载图像:

public class ImageAdapter extends PagerAdapter {
Context context;
public List<String> imgs = new ArrayList<String>();
ImageAdapter(Context context){
    this.context=context;
}
public void setImages(List<String> images){
    this.imgs = images;
}
@Override
public int getCount() {
    return imgs.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((ImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = new ImageView(context);
    imageView.setPadding(0, 0, 0, 0);
    imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
    Drawable d = new BitmapDrawable(context.getResources(), generateBitmap(context, imgs.get(position), 699, 699));
    imageView.setImageDrawable(d);
    ((ViewPager) container).addView(imageView, 0);
    return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((ImageView) object);
}
public static Bitmap generateBitmap(Context ctx, String path, double maxHeight,double maxWidth) {
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    InputStream bitmapIs = null;
    DisplayMetrics metrics = ctx.getResources().getDisplayMetrics();
    maxHeight = metrics.density * maxHeight;
    maxWidth  = metrics.density * maxWidth;
    try {
        bitmapIs = ctx.getAssets().open(path);
    } catch (Exception e) {e.printStackTrace();}

    o.inPreferredConfig = Bitmap.Config.RGB_565;
    BitmapFactory.decodeStream(bitmapIs, null, o);
    // The new size we want to scale to
    int REQUIRED_SIZE = 0;
    if (maxWidth > maxHeight) {
        REQUIRED_SIZE = (int) maxWidth;
    } else {
        REQUIRED_SIZE = (int) maxHeight;
    }
    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth;
    int height_tmp = o.outHeight;
    int sampleSize = 1;
    while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        sampleSize++;
    }
    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = sampleSize;
    o2.inPreferredConfig = Bitmap.Config.RGB_565;
    Bitmap originalBmp = BitmapFactory.decodeStream(bitmapIs, null, o2);
    // Find the correct scale value. It should be the power of 2.
    int origHeight = originalBmp.getHeight();
    int origWidth = originalBmp.getWidth();
    double scale = 1.0;
    double tmpScaleHeight = maxHeight / (double) origHeight;
    double tmpScaleWidth = maxWidth / (double) origWidth;
    if (tmpScaleHeight < tmpScaleWidth) {
        scale = tmpScaleHeight;
    } else {
        scale = tmpScaleWidth;
    }
    int scaledW = (int) (scale * origWidth);
    int scaledH = (int) (scale * origHeight);
    Bitmap tmpBmp = Bitmap.createScaledBitmap(originalBmp, scaledW,
            scaledH, true);
    originalBmp.recycle();
    return tmpBmp;
}

}

01-10 14:14:18.245: E/AndroidRuntime(6992): FATAL EXCEPTION: main
01-10 14:14:18.245: E/AndroidRuntime(6992): Process: de.abcdesign.abcdesignkatalog2014, PID: 6992
01-10 14:14:18.245: E/AndroidRuntime(6992): java.lang.OutOfMemoryError
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.content.res.Resources.loadDrawable(Resources.java:2110)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.content.res.Resources.getDrawable(Resources.java:700)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.widget.ImageView.resolveUri(ImageView.java:638)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.widget.ImageView.setImageResource(ImageView.java:367)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at de.abcdesign.abcdesignkatalog2014.Category.onCreate(Category.java:163)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.app.Activity.performCreate(Activity.java:5231)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.os.Handler.dispatchMessage(Handler.java:102)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.os.Looper.loop(Looper.java:136)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at java.lang.reflect.Method.invoke(Method.java:515)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-10 14:14:18.245: E/AndroidRuntime(6992):     at dalvik.system.NativeStart.main(Native Method)

尝试使用以下代码减小图像大小。 它将为我克服内存不足错误 enter code here

            Bitmap myBitmap;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPurgeable = true;
    options.outHeight = 50;
    options.outWidth = 50;
    options.inSampleSize = 4;
    myBitmap = BitmapFactory.decodeFile(path, options);
    // Bitmap bitmap = BitmapFactory.decodeFile(bean);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    myBitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);

在清单中添加largeheap设置为 true 的属性。那么你将不会收到此错误。

但是,您还应该使用Bitmap.Compress方法压缩图像

最新更新