android – 如果我选择一个大图像,OnActivityResult会崩溃

android – 如果我选择一个大图像,OnActivityResult会崩溃,第1张

概述我必须在图库中获取图像的图像路径,以便将其保存为列表,不幸的是,如果所选图像太大,则以下代码会使应用程序崩溃(并且不会查看我的try catch块中的警报). private void openImage() { try{ Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.se 我必须在图库中获取图像的图像路径,以便将其保存为列表,不幸的是,如果所选图像太大,则以下代码会使应用程序崩溃(并且不会查看我的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.access00(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)

如何解决这个问题?

解决方法 从 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,options);}mImageVIEw.setimageBitmap(decodeSampledBitmapFromresource(getResources(),R.ID.myimage,100,100));

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

总结

以上是内存溢出为你收集整理的android – 如果我选择一个大图像,OnActivityResult会崩溃全部内容,希望文章能够帮你解决android – 如果我选择一个大图像,OnActivityResult会崩溃所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1124968.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存