Weird behaviour while trying to get picture from DCIM

230 views Asked by At

I am trying to load a picture in my activity from DCIM. I use the following code :

int BROWSE_PICTURES = 0;
public void openBrowsePictures() {
    Intent i = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    startActivityForResult(i, BROWSE_PICTURES);
}

and in onActivityResult :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == BROWSE_PICTURES && resultCode == RESULT_OK && null != data) { // we have bitmap from filesystem!
        Uri selectedImage = data.getData();
        Log.d("CAMERA","____"+selectedImage.toString());


        String[] filePathColumn = {MediaStore.Images.Media.DATA};



        Cursor cursor = getContentResolver().query(
                selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        Log.d("CAMERA", " column : " + columnIndex);
        String filePath = cursor.getString(columnIndex);
        cursor.close();


        Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);


        Log.d("CAMERA", "----" + filePath);
    }
}

The situation becomes strange when I try to load a taken picture from filesystem. It works as expected when a. I choose a picture from EasyScreenshot file but when b. I choose picture from DCIM/Camera path it does not work. If I run the code the Log.d at the a. case prints :

CAMERA﹕ ____content://media/external/images/media/27487

and the second Log.d :

CAMERA﹕ ----/storage/emulated/0/Pictures/Screenshots/Screenshot_2014-12-18-15-14-22.png

but , in case b it prints the following :

the first log.d :

CAMERA: ____content://com.google.android.apps.photos.content/0/https%3A%2F%2Flh5.googleusercontent.com%2F7tUacBA_4oYS2Q8CmkINWHa93B_n7heNyt3OyVZgkY8%3Ds0-d

and the second log.d :

CAMERA﹕ ----null

I test the application in a nexus 4 device running Android 5.0.1

Thank you in advance

2

There are 2 answers

3
matiash On BEST ANSWER

You cannot assume that the Uri returned by the media picker will correspond to a local file. It looks like you're selecting a G+ photo or some other image that is not in the device.

The correct way to go would be to use a ContentResolver to access the picture as a stream. For example:

InputStream inputStream = null;
if (ContentResolver.SCHEME_CONTENT.equals(selectedImage.getScheme())) {
    inputStream = context.getContentResolver().openInputStream(selectedImage);
} else if (ContentResolver.SCHEME_FILE.equals(selectedImage.getScheme())) {
    inputStream = new FileInputStream(selectedImage.getPath());
}

bitmap = BitmapFactory.decodeStream(inputStream);

That should work for both content:// or file:// uris.

And (very important) make sure to do this from a background thread (e.g. AsyncTask), otherwise you'll get a NetworkOnMainThreadException if the uri is a "remote" one.

1
John Cipponeri On

You have to handle the content://com.google.android.apps.photos.content type of URL with a content provider, as you are already. However you should try doing it this way, skipping the real file path and just getting the bitmap itself.

https://stackoverflow.com/a/19835603/4380308