0

I am trying to save an image, and alert the Gallery about the new image. The image is not immediately shown in the gallery, but it can take even a few hour to refresh. I use the following code:

    String title = "title";
    String r = MediaStore.Images.Media.insertImage(getContentResolver(), State.pictureWithDekor, title, title);
    Uri uri = Uri.parse(r);
    MediaScannerConnection.scanFile(this,
            new String[] { uri.getPath() }, null,
            new MediaScannerConnection.OnScanCompletedListener() {

                public void onScanCompleted(String path, Uri uri) {
                    Log.i("TAG", "Finished scanning " + path);
                }
            });

the value of r after the insertImage is content://media/external/images/media/2082, the uri in the onScanCompleted is null.

What am I doing wrong?

Gábor Angyal
  • 2,225
  • 17
  • 27

1 Answers1

0

The problem is that the path returned from the MediaStore.Images.Media.insertImage is not a phisical path. to get the real path, use:

public String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

Source: Get filename and path from URI from mediastore

Community
  • 1
  • 1
Gábor Angyal
  • 2,225
  • 17
  • 27