Ilya Gazman
Ilya Gazman

Reputation: 32231

Android working with MediaStore

How can I open a file that I stored in gallery? I used this method to put my file there:

MediaStore.Images.Media.insertImage()

According to documentation it returns String url to the newly created file. However when I trying to open this file it fails. Getting file not found exception.

What am I missing? Any ideas?

Upvotes: 1

Views: 1076

Answers (1)

Antrromet
Antrromet

Reputation: 15414

It returns the String url or the content uri string back. To get the actual file path from the uri you need to use something like

protected String convertMediaUriToPath(Uri uri) {
    String [] proj={MediaStore.Images.Media.DATA};
    Cursor cursor = getContentResolver().query(uri, proj,  null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String path = cursor.getString(column_index); 
    cursor.close();
    return path;
}

You can convert the string url that you get to the Uri using Uri.parse(url);

Upvotes: 2

Related Questions