user27799 user27799 - 1 month ago 20
Android Question

Get file path from Uri created via FileProvider

I use this method for getting file path from Uri https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java#L257

But it throw exception ("_data" column not found) when I pass Uri created such as below:

public static Uri uriFromFile(Context context, String path) {
if (path == null) return null;
return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", new File(path));
}


I just need pass file path between my activities..

Answer

I use this method for getting file path from Uri

That is an unreliable approach that only works for a small number of Uri values.

But it throw exception ("_data" column not found) when I pass Uri created such as below:

Of course. That code will fail for most sources of Uri values.

Get file path from Uri created via FileProvider

In this specific case, you already know the "file path". It is represented by the variable named path in your code snippet.

More generally, you cannot get a "file path" for a Uri, for the simple reason that there is no requirement that a Uri point to a file, let alone one that you can access. Use ContentResolver and methods like openInputStream() to access the content represented by the Uri.

Comments