Sdcard File not open in OS 6 and later

2019-07-14 02:06发布

问题:

I have one method which open files from my app and this method running well on every OS for Internal Storage but when sdcard from OS 6 and upper want to open the file then I found an error :

Failed to find the configured root that contains /storage/BE02-07BA/WhatsApp/Media/WallPaper/download (1).jpg

My code is below :

try {
                    File f = new File(feedItem.getFilePath());
                    MimeTypeMap map = MimeTypeMap.getSingleton();
                    String url = f.getName();
                    url = URLEncoder.encode(url, "UTF-16").replace("+", "%20");
                    String ext = MimeTypeMap.getFileExtensionFromUrl(url);
                    String type = map.getMimeTypeFromExtension(ext.toLowerCase());
                    if (type == null)
                        type = "*/*";
                    Uri uri = Uri.parse("www.google.com");
                    Intent type_intent = new Intent(Intent.ACTION_VIEW, uri);
                    Uri data = FileProvider.getUriForFile(mContext, BuildConfig.APPLICATION_ID + ".provider",f);
                    type_intent.setDataAndType(data, type);
                    type_intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

                    mContext.startActivity(type_intent);
                } catch (Exception e) {
                    // Auto-generated catch block
                    e.printStackTrace();
                }

回答1:

Try to put this on your android manifest.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />