Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 7 nougat, how to get the file path from uri of incoming intent?

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.
like image 266
hohoins Avatar asked Apr 03 '17 07:04

hohoins


2 Answers

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

like image 143
John Avatar answered Sep 28 '22 00:09

John


// 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();
}
like image 26
satyawan hajare Avatar answered Sep 28 '22 02:09

satyawan hajare