4

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..

Nickolay Savchenko
  • 1,474
  • 16
  • 28

1 Answers1

5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 7
    @user27799: `FileProvider` is only useful for delivering content *to other apps*. If you are not doing that, get rid of `FileProvider`. If your concern is the `FileUriExposedException`, do not put a `Uri` in the `Intent`, but instead put a `String` extra that contains the file path (e.g., `path.getAbsolutePath()`), or pass the `File` object itself as a `Serializable` extra. One or both of those should avoid the `FileUriExposedException`. – CommonsWare Oct 10 '16 at 16:21