Search code examples
androidandroid-intentandroid-sharing

Easy and universal way to get filename shared with ACTION_SEND


I'm receiving intents sent with default share mechanism (ACTION_SEND) from different applications.

If it is a file – I need to copy it.
I don't need a full path to file since I can open it directly with a stream.

InputStream input = getContentResolver().openInputStream(fileUri);

But I need to know a filename to save file properly.

Is there any easy and universal way to get a filename of the shared file not paying attention to the content type (it can be image from gallery, video, files from downloads)?


Or should I use something like this to get the full path from the uri and then get filename from it?

Get real path from URI, Android KitKat new storage access framework [duplicate]
Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT


Also I saw this article (A Uri Is Not (Necessarily) a File) and there is said:

You can perform the following operations on such Uri values:

  • Find out their DISPLAY_NAME and SIZE — the OpenableColumns — via a query() on the Uri (again, using a ContentResolver, or in theory a CursorLoader)

Is it possible? How can I do that?


Solution

  • According to the MediaStore interface the DISPLAY_NAME column is indeed delivered.

    Cursor cursor = getContentResolver().query(fileUri, projection, null, null, null);
    

    projection contains DISPLAY_NAME

    cursor.moveToFirst();
    if (cursor.getCount() > 0) {
        // get your file name
    }
    

    I have not tested it but it should work ;-)