【问题标题】:OnActivityResult crash if I choose a large image如果我选择大图像,OnActivityResult 会崩溃
【发布时间】:2013-07-26 15:59:08
【问题描述】:

我必须获取图库中图像的图像路径,才能将其保存为列表,不幸的是,如果所选图像太大,以下代码会使应用程序崩溃(并且未查看我的 try catch 块中的警报) .

private void openImage() {

         try{
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.setType("image/*");
            startActivityForResult(i, FILE_REQ_CODE);
            } catch (RuntimeException e) {
                           e.printStackTrace();
               Alerts.TooLargeImage(LoadImage.this);
            }

    }

    protected void onActivityResult(int requestCode, int resultCode,
            Intent intentData) {
        try{
            Uri tmp = intentData.getData();
            String path = tmp.toString();
            imagePathList.add(path);
            preview.setImageURI(tmp);
            FileArchiveManager.saveImagePath(imagePathList);
            super.onActivityResult(requestCode, resultCode, intentData);
            } catch (RuntimeException e) {
                           e.printStackTrace();
               Alerts.TooLargeImage(LoadImage.this);
            }
    }

错误日志

java.lang.OutOfMemoryError: bitmap size exceeds VM budget(Heap Size=8135KB, Allocated=3718KB, Bitmap Size=11707KB)
    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:694)
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:494)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
    at android.widget.ImageView.resolveUri(ImageView.java:592)
    at android.widget.ImageView.setImageURI(ImageView.java:313)
    at com.myapp.LoadImage.onActivityResult(LoadImage.java:131)
    at android.app.Activity.dispatchActivityResult(Activity.java:4108)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3016)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3072)
    at android.app.ActivityThread.access$2000(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1084)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:150)
    at android.app.ActivityThread.main(ActivityThread.java:4385)
    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:849)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    at dalvik.system.NativeStart.main(Native Method)

如何解决这个问题?

【问题讨论】:

    标签: android android-activity android-image


    【解决方案1】:

    来自http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {
    
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);
    
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }
    
    mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
    

    使用BitmapFactory.decodeResource()BitmapFactory.Options.inSampleSize 将允许您将位图的下采样版本加载到RAM 中(因为实际上您不需要显示这么大的图像)而不会导致java.lang.OutofMemoryError

    【讨论】:

    • 感谢您的回答,但我不明白如何在我的方法中使用您的代码
    • 应用程序因 java.io.EOFException 而崩溃,如果我尝试使用 setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));在 onActivityResult
    • 你能给我更多关于 EOFException 的细节吗?
    • 问题似乎在 getResources() 中。如何从 Uri tmp 获取资源?
    【解决方案2】:

    如果您使用的是 android 3.0 及以后版本,请在清单文件的应用程序标记中使用以下代码

    应用程序 android:largeHeap="true"

    【讨论】:

    • 如果你想帮助这个人,请详细说明你的答案。
    【解决方案3】:

    我也遇到了同样的问题,我刚刚使用了 Glide。我需要 640x640 图像作为任何 1:1 比例图像代码的输出:

    if (resultCode == RESULT_OK) {
      Uri selectedImage = result.getUri();
        Glide.with(getApplicationContext()).asBitmap().load(selectedImage).override(640,640).into(new SimpleTarget<Bitmap>() {
        @Override
        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
          bitmap = resource;
          logoImage.setImageBitmap(bitmap);
        }
      });
    }
    

    使用最大 40mb 的图像文件进行测试

    【讨论】:

      猜你喜欢
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多