Search code examples
fileuriandroid-fileandroid-fileprovider

Can't get URI for selecting any file from download folder in MI-Oreo(MI-A2) device in android


I need help in fetching file path of contents that we pick from downloads folder or any other folder through Intent. I am attaching a fileUtil class that I have been using and it works well till Nougat version. But with oreo and that too in some specific devices, we receive a null path in return. So if anyone has faced such an error and found a solution to this, then please share here.

if(resultCode ==RESULT_OK)
{
    final Uri uri = data.getData();
    // Get the File path from the Uri
    String path = FileUtils.getPath(this, uri);
    // Alternatively, use FileUtils.getFile(Context, Uri)
    if (path != null && FileUtils.isLocal(path)) {
        //File file = new File(path);
        this.file = new File(path);
        profile_imagepath = path;
        filename = file.getName();
        txtselectedfile.setText(filename);
    }
}

Solution

  • You can try to use ACTION_PICK for api level above 26 to get this issue fixed, instead of using ACTION_GET_CONTENT. Try and hope this helps you.