如何正确编辑图库中的位图?

How to edit Bitmap from gallery correctly?

在我的应用程序中,我从 phone 图库获取图像,将其转换为位图并更改其尺寸以匹配我正在与之混合的另一个位图。当我从 Internet 下载图片并从我的应用程序中的图库访问它时,一切正常,但是当我从图库中选择一张在 phone 上拍摄的图片时,应用程序崩溃了。

我的代码:

            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.access0(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]  

请参考下面link,关于OOM的讨论很多

android - out of memory exception when creating bitmap

位图大小会很大,这就是您的应用程序因内存不足而崩溃的原因。在这种情况下,您可以在将位图加载到应用程序之前压缩位图,这可能会帮助您避免内存不足错误。