0

I have an activity that when users press "share" on a file it would open my app and start uploading the file. Now this works perfectly with images because the URI returned is for MediaStore. But I want to be able to return the URI from any source such as from ES File Explorer

Here is the current code:

    public String getPath(Uri uri) {
      String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
    }

How can I make this so instead of MediaStore it will be used for any type of file?

Zakukashi
  • 566
  • 1
  • 13
  • 29

2 Answers2

1

I'd recommend skipping the path step (if possible) and going straight to getting an InputStream which can simplify things a bit:

public InputStream getInputStream(Uri uri) {
    InputStream stream = null;
    String scheme = uri.getScheme();
    try {
        if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_FILE.equals(scheme) || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {
            stream = getContentResolver().openInputStream(uri);
        } else if ("https".equals(scheme) || "http".equals(scheme)) {
            // ContentResolver can't handle web uris. Handle or skip them as you see fit.
        }           
    } catch (FileNotFoundException e) {
        // Handle the exception however you see fit.
    }
    return stream;
}

I'd favor using the ContentResolver and letting it sort out the specifics... For example, what happens if you get a content uri that isn't related to the MediaStore? By letting the ContentResolver handle that, you don't have to care.

nEx.Software
  • 6,782
  • 1
  • 26
  • 34
0

You can't do that. You can use it for the media store, beacuse the path of those images are stored in the built in SQLite database, but the files on your sd card has no entries in the database.

You can check if the URI's scheme is content or file, and use different methods to access the file the following way:

public String getPath(Uri uri) {
        if(uri.getScheme().equals("content")){
                String[] projection = { MediaStore.Images.Media.DATA };
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow(
                        MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                return cursor.getString(column_index);
        } else if(uri.getScheme().equals("file")){
                File myFile = new File(uri);
                return myFile.getAbsolutePath();
        }
}
Adam Monos
  • 4,287
  • 1
  • 23
  • 25
  • Thank you, worked like a charm but with some minor modifications. The code above did not let me compile until I took away "if(uri.getScheme().equals("file")" and put an extra parenthesis after (uri.getScheme().equals("content"), and I made File myFile = new File(uri.getPath()); – Zakukashi Jul 02 '12 at 15:28
  • Oh, sorry about those typos, I updated my answer. Thanks for pointing it out. – Adam Monos Jul 02 '12 at 15:57