gpt4 book ai didi

android - 从 SD 卡读取图像时出现内存不足错误

转载 作者:行者123 更新时间:2023-11-29 02:15:27 25 4
gpt4 key购买 nike

我正在开发一个带有 ImageView 的简单应用程序,当单击 ImageView 时,应用程序应打开设备图片库。为此,我使用以下代码

    Intent imageIntent=new Intent(Intent.ACTION_PICK);
imageIntent.setType("image/*");
startActivityForResult(imageIntent,5);

和 ActivityResult

super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 5)
{
Cursor cursor;
Uri uri=data.getData();
if(uri!=null)
{
cursor=getContentResolver().query(uri, new String[]{ android.provider.MediaStore.Images.ImageColumns.DATA },
null, null, null);
cursor.moveToFirst();
String imageID=cursor.getString(0);
personImage.setImageURI(uri);
Log.i("ImageId", ""+imageID);
cursor.close();
}
}

所选图像显示在 ImageView 中。现在的问题是当我第二次这样做时出现以下异常。

12-01 19:31:52.344: ERROR/AndroidRuntime(2011): FATAL EXCEPTION: main
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:468)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:332)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.widget.ImageView.resolveUri(ImageView.java:572)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.widget.ImageView.setImageURI(ImageView.java:293)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at com.css.testsample.ReadImage.onActivityResult(ReadImage.java:36)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.app.Activity.dispatchActivityResult(Activity.java:3931)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.app.ActivityThread.access$2800(ActivityThread.java:135)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.os.Looper.loop(Looper.java:144)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at android.app.ActivityThread.main(ActivityThread.java:4937)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at java.lang.reflect.Method.invokeNative(Native Method)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at java.lang.reflect.Method.invoke(Method.java:521)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): at dalvik.system.NativeStart.main(Native Method)

我不知道如何处理这个问题,如果有人知道,请帮助我。

感谢和问候,拉贾潘迪安.K

最佳答案

尝试对图像进行下采样,如图所示 here

关于android - 从 SD 卡读取图像时出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4325201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com