gpt4 book ai didi

android - 从图库(SD 卡)中选择图像用于我的应用程序时出现异常...java.lang.OutOfMemoryError : bitmap size exceeds VM budget

转载 作者:行者123 更新时间:2023-11-29 02:14:53 24 4
gpt4 key购买 nike

我正在使用 android 在我的应用程序上选择图像,我不知道为什么,有时我会遇到异常,我认为我总是选择超过 400 或 500 kb 的图像,但我不确定。为什么?因为当我选择 100 kb 的小图像时,我没有得到异常,当我得到具有高 KB 或 MB 的图像时,它因 java.lang.OutOfMemoryError: bitmap size exceeds VM budget 而崩溃。 但非常重要:它在模拟器上崩溃但也在我的手机上崩溃,这不是模拟器的故障。

这是我的代码:

changeImageButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
}
});



protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

switch(requestCode) {
case 1:
{
setResult(1);
finish();
}
case ACTIVITY_SELECT_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();

selectedPhoto = BitmapFactory.decodeFile(filePath);
//profileImage.setImageBitmap(selectedPhoto);
profileImage.setImageBitmap(Bitmap.createScaledBitmap(selectedPhoto, 80, 80, false));
}
}
}

profileImage 是我布局的一个 ImageView。我使用缩放的 butmap 将图像调整为 80x80

请帮我解决这个异常,我需要解决它

这是异常(exception)情况:

 01-31 12:38:37.531: ERROR/AndroidRuntime(1025): Uncaught handler: thread main exiting due to uncaught exception
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:375)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:171)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:196)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at com.GPSLoc.Configuration.onActivityResult(Configuration.java:253)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.app.Activity.dispatchActivityResult(Activity.java:3595)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.app.ActivityThread.deliverResults(ActivityThread.java:3001)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3047)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.app.ActivityThread.access$2300(ActivityThread.java:112)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.os.Handler.dispatchMessage(Handler.java:99)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.os.Looper.loop(Looper.java:123)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at android.app.ActivityThread.main(ActivityThread.java:3948)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at java.lang.reflect.Method.invokeNative(Native Method)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at java.lang.reflect.Method.invoke(Method.java:521)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
01-31 12:38:37.552: ERROR/AndroidRuntime(1025): at dalvik.system.NativeStart.main(Native Method)

最佳答案

我研究过的 Bitmap.createScaledBitmap 方法内存消耗很高并会产生该错误,但是,我对此并不完全确定。无论如何,它是 JAVA API,您不能更深入。

您可以执行下一步:

//create a Drawable with your image as parameter
BitmapDrawable d= new BitmapDrawable(youBitmap);
//define bounds for your drawable
int left =0;
int top = 0;
int right=80;
int bottom=80;

Rect r = new Rect(left,top,right,bottom);
//set the new bounds to your drawable
d.setBounds(r);
//set the drawable as view of your image view
profileImage.setImageDrawable(d);

我没有测试过这段代码,但它应该可以工作。

关于android - 从图库(SD 卡)中选择图像用于我的应用程序时出现异常...java.lang.OutOfMemoryError : bitmap size exceeds VM budget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4850985/

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