在我的应用程序中,我从手机图库中获取图像,将其转换为位图,然后更改其尺寸以匹配与之混合的其他位图。当我从互联网上下载图片并从我的应用程序的图库中访问它时,一切正常,但是当我从通过手机拍摄的图库中选择一张图片时,该应用程序崩溃了。
我的代码:
String stringUri = null;
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey("KEY")) {
stringUri= extras.getString("KEY");
}
Uri uri;
uri = Uri.parse(extras.getString("KEY"));
try {
bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bmp=getResizedBitmap(bmp, operation.getWidth(), operation.getHeight());
bmp.setDensity(c.getDensity());
myView = new MainView(this, operation, bmp,brush);
FrameLayout preview = (FrameLayout) findViewById(R.id.preview);
preview.addView(myView);
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
这是我的堆栈:
Thread [<1> main] (Suspended (exception OutOfMemoryError))
<VM does not provide monitor information>
BitmapFactory.decodeStream(InputStream, Rect, BitmapFactory$Options) line: 650
BitmapFactory.decodeStream(InputStream) line: 722
MediaStore$Images$Media.getBitmap(ContentResolver, Uri) line: 788
Blend.onCreate(Bundle) line: 106
Blend(Activity).performCreate(Bundle) line: 5206
Instrumentation.callActivityOnCreate(Activity, Bundle) line: 1083
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2064
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 2125
ActivityThread.access$600(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 140
ActivityThread$H.handleMessage(Message) line: 1227
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4898
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 1006
ZygoteInit.main(String[]) line: 773
NativeStart.main(String[]) line: not available [native method]
最佳答案
请参考下面的链接,关于OOM的讨论很大
Strange out of memory issue while loading an image to a Bitmap object
https://stackoverflow.com/questions/5697760/android-out-of-memory-exception-when-creating-bitmap
关于java - 如何从图库中正确编辑位图?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29206415/