How to Extract the File Name from Uri Returned from Intent.Action_Get_Content

How to get the file name from an URI got from ACTION_GET_CONTENT Intent?

I need to get the name of the file.

It is not a file. It is a Uri, just like https://stackoverflow.com/questions/43615264/how-to-get-the-file-name-from-an-uri-got-from-action-get-content-intent is a URI. how-to-get-the-file-name-from-an-uri-got-from-action-get-content-intent is not a filename.

What am I doing wrong?

You are assuming that ACTION_GET_CONTENT gives you a file.

You are welcome to use a ContentResolver to query() for the OpenableColumns and ask for the DISPLAY_NAME. I demonstrate that in this sample app. However, please understand that you are getting a display name, and it is up to the app that the user chooses to handle ACTION_GET_CONTENT what the "display name" actually is. It might be a filename, but it does not have to be.

Getting filename from uri

This is the code I'm using to get informations from a Uri :

public static class FileMetaData
{
public String displayName;
public long size;
public String mimeType;
public String path;

@Override
public String toString()
{
return "name : " + displayName + " ; size : " + size + " ; path : " + path + " ; mime : " + mimeType;
}
}

public static FileMetaData getFileMetaData(Context context, Uri uri)
{
FileMetaData fileMetaData = new FileMetaData();

if ("file".equalsIgnoreCase(uri.getScheme()))
{
File file = new File(uri.getPath());
fileMetaData.displayName = file.getName();
fileMetaData.size = file.length();
fileMetaData.path = file.getPath();

return fileMetaData;
}
else
{
ContentResolver contentResolver = context.getContentResolver();
Cursor cursor = contentResolver.query(uri, null, null, null, null);
fileMetaData.mimeType = contentResolver.getType(uri);

try
{
if (cursor != null && cursor.moveToFirst())
{
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
fileMetaData.displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

if (!cursor.isNull(sizeIndex))
fileMetaData.size = cursor.getLong(sizeIndex);
else
fileMetaData.size = -1;

try
{
fileMetaData.path = cursor.getString(cursor.getColumnIndexOrThrow("_data"));
}
catch (Exception e)
{
// DO NOTHING, _data does not exist
}

return fileMetaData;
}
}
catch (Exception e)
{
Log.e(Log.TAG_CODE, e);
}
finally
{
if (cursor != null)
cursor.close();
}

return null;
}
}

URI from Intent.ACTION_GET_CONTENT into File

I've done this method to convert Uri from Intent.ACTION_GET_CONTENT to real path:

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

Which in turn converted into File:

Uri filePathFromActivity = (Uri) extras.get(Intent.EXTRA_STREAM);
filePathFromActivity = Uri.parse(FileUtil.getRealPathFromUri( (Activity) IntentActivity.this, filePathFromActivity));
File imageFile = new File(filePathFromActivity.getPath());

ContentResolver - how to get file name from Uri

You can get file name from this code, or any other field by modifying the projection

String[] projection = {MediaStore.MediaColumns.DATA};

ContentResolver cr = getApplicationContext().getContentResolver();
Cursor metaCursor = cr.query(uri, projection, null, null, null);
if (metaCursor != null) {
try {
if (metaCursor.moveToFirst()) {
path = metaCursor.getString(0);
}
} finally {
metaCursor.close();
}
}
return path;

ACTION_GET_CONTENT Picking Document from Downloads returns null

I used use below code for

if (isDownloadsDocument(uri)) {
String fileName = getFilePath(context, uri);
if (fileName != null) {
return Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
}

final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}

And getFilePath()

public static String getFilePath(Context context, Uri uri) {

Cursor cursor = null;
final String[] projection = {
MediaStore.MediaColumns.DISPLAY_NAME
};

try {
cursor = context.getContentResolver().query(uri, projection, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}

refer: This link



Related Topics



Leave a reply



Submit