Reputation: 1858
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.
Upvotes: 3
Views: 3003
Reputation: 10274
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));
Upvotes: 3
Reputation: 1007369
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.
Upvotes: 5