Media not found on android 11 while trying to open file using ACTION_VIEW

不问归期 提交于 2021-01-29 08:08:46

问题


I have a method that I am using to open file from my application and it is all working fine till android 10 but when I try to open file in android 11 using google photos, it does not open it and shows Media not found toast. I am trying to open in any external app.

Here is my method that I am using:

private void openFile(String url) throws IOException {
    // Create URI
    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
    Uri uri = Uri.parse(url);

    Intent intent = null;
    // Check what kind of file you are trying to open, by comparing the url with extensions.
    // When the if condition is matched, plugin sets the correct intent (mime) type,
    // so Android knew what application to use to open the file
    intent = new Intent(Intent.ACTION_VIEW);
    if (url.contains(".doc") || url.contains(".docx")) {
        // Word document
        intent.setDataAndType(uri, "application/msword");
    } else if(url.contains(".pdf")) {
        // PDF file
        intent.setDataAndType(uri, "application/pdf");
    } else if(url.contains(".ppt") || url.contains(".pptx")) {
        // Powerpoint file
        intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
    } else if(url.contains(".xls") || url.contains(".xlsx")) {
        // Excel file
        intent.setDataAndType(uri, "application/vnd.ms-excel");
    } else if(url.contains(".rtf")) {
        // RTF file
        intent.setDataAndType(uri, "application/rtf");
    } else if(url.contains(".wav")) {
        // WAV audio file
        intent.setDataAndType(uri, "audio/x-wav");
    } else if(url.contains(".gif")) {
        // GIF file
        intent.setDataAndType(uri, "image/gif");
    } else if(url.contains(".jpg") || url.contains(".jpeg")) {
        // JPG file
        intent.setDataAndType(uri, "image/jpeg");
    } else if(url.contains(".png")) {
        // PNG file
        intent.setDataAndType(uri, "image/png");
    } else if(url.contains(".txt")) {
        // Text file
        intent.setDataAndType(uri, "text/plain");
    } else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) {
        // Video files
        intent.setDataAndType(uri, "video/*");
    }

    //if you want you can also define the intent type for any other file

    //additionally use else clause below, to manage other unknown extensions
    //in this case, Android will show all applications installed on the device
    //so you can choose which application to use


    else {
        intent.setDataAndType(uri, "*/*");
    }

    this.cordova.getActivity().startActivity(intent);
}

来源:https://stackoverflow.com/questions/64678244/media-not-found-on-android-11-while-trying-to-open-file-using-action-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!