Android 7 nougat, how to get the file path from ur

2020-06-19 05:12发布

FileProvider:- Setting up file sharing

I know that change the file policy in android nougat. The wanted app that file share to other apps generate the uri by FileProvider. The uri format is content://com.example.myapp.fileprovider/myimages/default_image.jpg.

I want know that how can I get filepath from the uri that generate by FileProvider.getUriForFile(). because the my app is need to know the physical filepath in order to save, load, readinfo, etc. is it possible

[In short]

  1. My app received the intent uri by other apps on andorid 7 nougat.
  2. The uri format is content://com.example.myapp.fileprovider/myimages/default_image.jpg
  3. Maybe it was generate by FileProvider.getUriForFile.
  4. I want to know way that get the file path from uri.
  5. I can just get the mime type, display name, file size and read binay from getContentResolver().openFileDescriptor(). but I wnat to know the filepath.

3条回答
我欲成王,谁敢阻挡
2楼-- · 2020-06-19 05:24

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. Use ContentResolver and methods like openInputStream() to access the content represented by the Uri.

To share a file with another app using a content URI, your app has to generate the content URI. To generate the content URI, create a new File for the file, then pass the File to getUriForFile(). You can send the content URI returned by getUriForFile() to another app in an Intent. The client app that receives the content URI can open the file and access its contents by calling ContentResolver.openFileDescriptor to get a ParcelFileDescriptor. Source: Android developers

查看更多
干净又极端
3楼-- · 2020-06-19 05:27
// Use Below Method Working fine for Android N.
private static String getFilePathForN(Uri uri, Context context) {
    Uri returnUri = uri;
    Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
    /*
     * Get the column indexes of the data in the Cursor,
     *     * move to the first row in the Cursor, get the data,
     *     * and display it.
     * */
    int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
    int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
    returnCursor.moveToFirst();
    String name = (returnCursor.getString(nameIndex));
    String size = (Long.toString(returnCursor.getLong(sizeIndex)));
    File file = new File(context.getFilesDir(), name);
    try {
        InputStream inputStream = context.getContentResolver().openInputStream(uri);
        FileOutputStream outputStream = new FileOutputStream(file);
        int read = 0;
        int maxBufferSize = 1 * 1024 * 1024;
        int bytesAvailable = inputStream.available();

        //int bufferSize = 1024;
        int bufferSize = Math.min(bytesAvailable, maxBufferSize);

        final byte[] buffers = new byte[bufferSize];
        while ((read = inputStream.read(buffers)) != -1) {
            outputStream.write(buffers, 0, read);
        }
        Log.e("File Size", "Size " + file.length());
        inputStream.close();
        outputStream.close();
        Log.e("File Path", "Path " + file.getPath());

    } catch (Exception e) {
        Log.e("Exception", e.getMessage());
    }
    return file.getPath();
}
查看更多
干净又极端
4楼-- · 2020-06-19 05:45

I had the similar requirement in our app and got confused earlier. I solved this way.

In Android N, only the file uri exposed to 3rd party app is changed. (Not the way we were using it before).

In our app we were sending uri to Camera app, in that location we are expecting the camera app to store the captured image.

  1. For android N, we generate new Content:// uri based url pointing to file.
  2. We generate usual File api based path for the same (using older method).

Now we have 2 different uri for same file. #1 is shared with Camera app. If the camera intent is success, we can access the image from #2.

Hope this helps.

查看更多
登录 后发表回答