Android7.0 uri转file问题

适配7.0的时候,我们需要编写

 permission_file_paths.xml


    

    

    

    

    

    

    


manifrest.xml


            
        

用到uri的时候,我们可能要转下

   public static Uri toUri(String filePath) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            return FileProvider.getUriForFile(App.getInstance(), App.getInstance().getPackageName() + ".fileprovider", new File(filePath));
        }
        return Uri.fromFile(new File(filePath));
    }

有的时候,我们还需要uri转回file,这时候,我们需要适配下

 public static File toFile(Uri uri) {
        String pathStr = uri.getPath();
        if (pathStr != null) {
            //7.0路径适配问题
            if (pathStr.startsWith("/external_path")) {
                pathStr = pathStr.replace("/external_path", Environment.getExternalStorageDirectory().getPath());
            }
            File file = new File(pathStr);
            return file;
        }
        return null;
    }

你可能感兴趣的:(Android7.0 uri转file问题)