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

时间:2015-03-23 09:04:44

标签: java android bitmap stack-trace

在我的应用中,我从手机图库中获取了一张图片,将其转换为位图并更改其尺寸以匹配我正在混合的另一个位图。当我从互联网上下载图片并从我的应用程序中的图库访问它时,一切正常,但是当我从电话中选择了一张图片时,应用程序就会崩溃。

我的代码:

            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]  

2 个答案:

答案 0 :(得分:1)

位图大小将非常大,这就是因为内存不足导致应用程序崩溃的原因。在这种情况下,您可以在应用程序中加载位图之前压缩位图,这可能会帮助您解决内存不足错误。

答案 1 :(得分:0)