3

In my app, user can pick image from camera and Gallery to load into ImageView.

I am using following code to open relevant application to choose image:

 Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);

Problem is with the Google drive. If user select an image from Google Drive, I am getting URI like this: content://com.google.android.apps.docs.storage.legacy/enc%3DHxtpLaMr66OMOzDOKTRZ-5ed7q7Szj7F7nC0IlxBnV8hx2P1%0A

I am able to generate Bitmap but unable to get file name.

 InputStream input = context.getContentResolver().openInputStream(uri);
               Bitmap bitmap = BitmapFactory
                        .decodeStream(input);

I need to show File name which user has selected.

Please, help me to extract image name from google drive URI.

Amiraslan
  • 794
  • 1
  • 8
  • 19
Rohan Patel
  • 1,758
  • 2
  • 21
  • 38

3 Answers3

5

I am able to generate Bitmap but unable to get file name

There does not have to be a filename. Not every sequence of bytes has a filename. For example, this answer does not have a filename.

Please help me to extract image name from google drive uri.

That is not strictly possible.

You could switch to ACTION_OPEN_DOCUMENT. Then, wrap the resulting Uri in a DocumentFile using fromSingleUri(). Then, call getName() on the DocumentFile. This will be a "display name", something that the user should recognize. It still may not be a filename.

I cannot rule out the possibility that the Google Drive API has some means of getting a display name from a Uri that you get from ACTION_GET_CONTENT, though that would surprise me. It is also possible that your Uri from ACTION_GET_CONTENT will work with DocumentFile — if you add CATEGORY_OPENABLE to your Intent, it would increase the odds that this works.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Checked in Kitkat, DocumentFile object is getting created but getName() method returning null. How can I get display name kitkat and below? – Rohan Patel Jun 26 '17 at 09:17
  • - check above comment – Rohan Patel Jun 26 '17 at 09:49
  • @RohanPatel: That may be a `DocumentFile` limitation. I have [a better version in this library](https://github.com/commonsguy/cwac-document). Or, you can `query()` for the `OpenableColumns` yourself using a `ContentResolver`. – CommonsWare Jun 26 '17 at 11:10
  • Thanks a lot Mark for the `fromSingleUri()` tip, does wonders on Google Drive files! – 0101100101 Jan 31 '18 at 07:47
3

Use the way provided and recommended by Google (https://developer.android.com/guide/topics/providers/document-provider):

Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null, null);
if (cursor != null && cursor.moveToFirst())
  String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
Gábor
  • 9,466
  • 3
  • 65
  • 79
0

Actually, I can recommend you two ways:

First way is that name the file yourself and save it to another URI in phone.

And the second way, you can get filename from uri itself. See the answer below:

Link

Amiraslan
  • 794
  • 1
  • 8
  • 19