Search code examples
androidbitmapimageviewurionactivityresult

Android: Intent in OnActivityResult giving a wrong path


In the android app that I am making a user takes a photo which I want to display him later in few activities. I've come up to "out of memory error" while displaying currently taken photo in an image view, so I decided to use code from http://developer.android.com/training/displaying-bitmaps/load-bitmap.html to do it efficiently. Here are methods that I'm using:

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;


        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

and

public static Bitmap decodeSampledBitmap(Uri mUri, int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mUri.getPath(), options);

    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(mUri.getPath, options);

};

I let the user take photo by the method:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent(){
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if(takePictureIntent.resolveActivity(getPackageManager()) != null){
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

And then receive taken picture info:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK){
        Uri imageUri = null;
        if(data != null){
            imageUri = data.getData();
        }

        ImageView imageView = (ImageView)findViewById(R.id.new_photo);
        imageView.setImageBitmap(decodeSampledBitmap(imageUri, imageView.getWidth(), imageView.getHeight()));


    }

}

My app breaks and I get info: "File not found or no such directory". I checked what does the imageUri.getPath() give and in my case its: "/external/images/media/1777" (which seems quite strange to me because I'm not using SD card) and the taken photo is actually saved in "/storage/emulated/0/DCIM/100ANDRO/DSC_0052.JPG". Do you have any ideas what am I doing wrong?


Solution

  • Intent Extra Max limit is 1MB approx till gingerbread..

    I found a magic number 86389 in JellyBean if you send above this it will throw memory out of exception..

    Solution : Pass Image Uri, dont pass complete bitmap object