微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

尝试使用ACTION_VIEW

如何解决尝试使用ACTION_VIEW

我有一种方法可以用来从应用程序中打开文件,并且在android 10之前都可以正常工作,但是当我尝试使用google照片在android 11中打开文件时,它无法打开并显示未找到媒体吐司。我正在尝试在任何外部应用程序中打开。

这是我使用的方法

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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。