异常采摘从画廊(SD卡),一个图像在我的应用程序使用... java.lang.OutOfMemoryError:位图的大小超过VM预算 [英] Exception picking a image from gallery (SD Card) for use on my app... java.lang.OutOfMemoryError: bitmap size exceeds VM budget
问题描述
我在我的应用程序采摘图像与Android,和我不知道为什么,有时我得到一个例外,我认为这是我永诺选择与超过400或500 KB的一个形象,但我不知道。为什么?因为当我选择100 KB的小图像我没有得到的异常,当我得到高KB或MB的图像,它崩溃java.lang.OutOfMemoryError:位图的大小超过VM预算
。 但很重要:它崩溃在模拟器上也发生崩溃我的电话,是不是模拟器的失败
这是我的code:
changeImageButton.setOnClickListener(新OnClickListener(){
公共无效的onClick(视图v){
意图I =新意图(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(ⅰ,ACTIVITY_SELECT_IMAGE);
}
});保护无效的onActivityResult(INT申请code,INT结果code,意图imageReturnedIntent){
super.onActivityResult(要求code,结果code,imageReturnedIntent); 开关(要求code){
情况1:
{
的setResult(1);
完();
}
案例ACTIVITY_SELECT_IMAGE:
如果(结果code == RESULT_OK){
乌里selectedImage = imageReturnedIntent.getData();
的String [] = filePathColumn {MediaStore.Images.Media.DATA}; 光标光标= getContentResolver()查询(selectedImage,filePathColumn,NULL,NULL,NULL);
cursor.moveToFirst(); INT参数:columnIndex = cursor.getColumnIndex(filePathColumn [0]);
字符串文件路径= cursor.getString(参数:columnIndex);
cursor.close(); selectedPhoto = BitmapFactory.de codeFILE(文件路径);
//profileImage.setImageBitmap(selectedPhoto);
profileImage.setImageBitmap(Bitmap.createScaledBitmap(selectedPhoto,80%,80,假));
}
}
}
profileImage是我布局的ImageView的。我用缩放butmap到resice图像以80×80
请给我一些帮助,这个例外,我需要去解决它。
这是个例外:
12月1日至31日:38:37.531:ERROR / AndroidRuntime(1025):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):java.lang.OutOfMemoryError:位图大小超过VM预算
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.graphics.BitmapFactory.nativeDe codeStream(本机方法)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.graphics.BitmapFactory.de codeStream(BitmapFactory.java:375)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:171)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.graphics.BitmapFactory.de codeFILE(BitmapFactory.java:196)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在com.GPSLoc.Configuration.onActivityResult(Configuration.java:253)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.app.Activity.dispatchActivityResult(Activity.java:3595)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.app.ActivityThread.deliverResults(ActivityThread.java:3001)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3047)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.app.ActivityThread.access $ 2300(ActivityThread.java:112)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1721)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.os.Handler.dispatchMessage(Handler.java:99)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.os.Looper.loop(Looper.java:123)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在android.app.ActivityThread.main(ActivityThread.java:3948)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在java.lang.reflect.Method.invokeNative(本机方法)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在java.lang.reflect.Method.invoke(Method.java:521)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:782)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
12月1日至31日:38:37.552:ERROR / AndroidRuntime(1025):在dalvik.system.NativeStart.main(本机方法)
Bitmap.createScaledBitmap方法,只要我已经研究了内存消耗的高潮,并产生一个错误,但是,我不能完全确定了。反正它是JAVA API,你不能走得更深。
您可以做下:
//创建映像,作为参数可绘制
BitmapDrawable D =新BitmapDrawable(youBitmap);
//定义界限为您绘制
INT左= 0;
INT顶= 0;
诠释权= 80;
INT底= 80;矩形R =新的矩形(左,上,右,下);
//设置新的边界您绘制
d.setBounds(R);
//设置绘制的视图中的图像视图
profileImage.setImageDrawable(四);
我没有测试此code,但它应该工作。
i am picking images on my app with android, and i dont know why, sometimes i got an exception, i think it's allways i select a image with more than 400 or 500 kb's but i am not sure. Why? because when i select small images of 100 kb i dont get the exception, and when i get images with high KB or MB, it crash WITH java.lang.OutOfMemoryError: bitmap size exceeds VM budget
. BUT VERY IMPORTANT: it crashes on the emulator but ALSO CRASH ON MY PHONE, is not a failure of the emulator.
this is my code:
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 is a ImageView of my layout. and i use scaled butmap to resice the image to 80x80
please give me some help with this exception, i need to solve it
this is the 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 method as long as I have researched has a highpoint of memory consumption and produces that error, but, I'm not totally sure about it. Anyway it is JAVA API and you cannot go deeper.
You can do the next:
//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);
I have not tested this code, but it should work.
这篇关于异常采摘从画廊(SD卡),一个图像在我的应用程序使用... java.lang.OutOfMemoryError:位图的大小超过VM预算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!