尝试处理图像时出现OutOfMemoryError

时间:2015-07-24 09:39:59

标签: android android-bitmap

我试图解码图像并改变图像的大小。

public static Bitmap decodeSampledBitmapFromResource(String picturePath,
                                                         int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(picturePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(picturePath, options);
    }

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

这是工作,但如果我试图在弱电话和大图片上做这件事我会收到错误

  07-24 12:02:03.622  24472-24472/com.skip.client.customer E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.skip.client.customer, PID: 24472
        java.lang.OutOfMemoryError
                at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
                at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613)
                at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589)
                at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:369)
                at com.skip.client.customer.utils.ImageHelper.decodeSampledBitmapFromResource(ImageHelper.java:33)
                at com.skip.client.customer.fragments.dialogs.ImageDialog.setData(ImageDialog.java:83)
                at com.skip.client.customer.fragments.dialogs.ImageDialog.onCreateView(ImageDialog.java:68)
                at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
                at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
                at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
                at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
                at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
                at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
                at android.app.Activity.performStart(Activity.java:5285)
                at android.app.Activity.performRestart(Activity.java:5341)
                at android.app.Activity.performResume(Activity.java:5346)
                at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2790)
                at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2829)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1245)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5118)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
                at dalvik.system.NativeStart.main(Native Method)

如何优化性能并减少内存消耗?

0 个答案:

没有答案