Jonah G
Jonah G

Reputation: 81

How to edit Bitmap from gallery correctly?

In my app I get an image from the phone gallery, turn it into a bitmap and change its dimensions to match another bitmap that I'm blending it with. When I download a pic from the internet and access it from the gallery in my app everything works but when I choose a pic from the gallery which was taken on the phone the app craches.

my code:

            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;
} 

And here's my Stack:

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]  

Upvotes: 0

Views: 75

Answers (2)

Samir Bhatt
Samir Bhatt

Reputation: 3261

Bitmap size will be so big that's why your application crash because of out of memory. In that case, You can compress bitmap before load it in application which may help you from out of memory error.

Upvotes: 1

Related Questions