3

I am using an intent to invoke a file chooser that is external to my app. Some of the file chooser applications return an Uri of scheme "content".

I need to obtain the last modified date of the chosen object. How do I do that when the scheme is "content"? I didn't find an appropriate API.

There is some API that returns a FileDescriptor. But I don't get the last modified date from a FileDescriptor. Any help appreciate.

Best Regards

  • is it your own custom ContentProvider ? – pskink Mar 08 '14 at 17:35
  • No, for example https://play.google.com/store/apps/details?id=com.speedsoftware.explorer delivers an Uri with content schema. I help myself by dropping the authority and changing the schema to file. But this doesn't work for example for https://play.google.com/store/apps/details?id=com.dropbox.android . –  Mar 08 '14 at 19:04

1 Answers1

3

In general you can't do what you want - there is no API to get a File for an item described by a "content" URI because a content URI does not have to correspond with a file anyway.

In practice it is possible to get a File path for some content URIs:

  • as you describe, sometimes you can get lucky, and manipulate the content uri by changing the content scheme to a file scheme

  • If the content URI came from the Media store, then you can do a query to get the file path

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

There are a whole host of other questions asking pretty much the same thing that provide further ideas (or slightly different working of the same ideas)

Community
  • 1
  • 1
zmarties
  • 4,809
  • 22
  • 39