Android《7.0适配》

第一步

        
            
        

第二部



    
    
    
    
    
    

第三部

public class FileProvider {
    public static Uri getUriForFile(Context context, File file) {
        Uri fileUri = null;
        if (Build.VERSION.SDK_INT >= 24) {
            fileUri = getUriForFile24(context, file);
        } else {
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }

    public static Uri getUriForFile24(Context context, File file) {
        Uri fileUri = android.support.v4.content.FileProvider.getUriForFile(context,
                context.getPackageName() + ".android7.fileprovider",
                file);
        return fileUri;
    }
}

第四部

    File file = null;
        Uri imageUri = null;
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        String filename = new SimpleDateFormat("yyyyMMdd-HHmmss", Locale.CHINA)
                .format(new Date()) + ".png";

        file = new File(filename);
        Log.e("root-path", file.getAbsolutePath());
        imageUri = FileProvider.getUriForFile(this, file);
        Log.e("root-path", imageUri.toString());
        Log.e("root-path", "---------------------------------------------------------");

        file = new File(getFilesDir()+"/files/", filename);
        Log.e("files-path", file.getAbsolutePath());
        imageUri = FileProvider.getUriForFile(this, file);
        Log.e("files-path", imageUri.toString());
        Log.e("files-path", "---------------------------------------------------------");

        file = new File(getCacheDir()+"/caches/", filename);
        Log.e("cache-path", file.getAbsolutePath());
        imageUri = FileProvider.getUriForFile(this, file);
        Log.e("cache-path",  imageUri.toString());
        Log.e("cache-path", "---------------------------------------------------------");

        file = new File(Environment.getExternalStorageDirectory()+"/sdfiles/", filename);
        Log.e("external-path", file.getAbsolutePath());
        imageUri = FileProvider.getUriForFile(this, file);
        Log.e("external-path", imageUri.toString());
        Log.e("external-path", "---------------------------------------------------------");

        file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)+"/sdfiles/", filename);
        Log.e("external-files-path", file.getAbsolutePath());
        imageUri = FileProvider.getUriForFile(this, file);
        Log.e("external-files-path", imageUri.toString());
        Log.e("external-files-path", "---------------------------------------------------------");

        file = new File(context.getExternalCacheDir()+"/sdfiles/", filename);
        Log.e("external-cache-path", file.getAbsolutePath());
        imageUri = FileProvider.getUriForFile(this, file);
        Log.e("external-cache-path", imageUri.toString());
        Log.e("external-cache-path", "---------------------------------------------------------");

第五步讲解对应关系

/com.bsoft.lwd.ferrysupport E/root-path: /20171106-165706.png                                                                                             
/com.bsoft.lwd.ferrysupport E/root-path: content://com.bsoft.lwd.ferrysupport.android7.fileprovider/root/20171106-165706.png                              
/com.bsoft.lwd.ferrysupport E/root-path: ---------------------------------------------------------                                                        
/com.bsoft.lwd.ferrysupport E/files-path: /data/user/0/com.bsoft.lwd.ferrysupport/files/20171106-165706.png                                               
/com.bsoft.lwd.ferrysupport E/files-path: content://com.bsoft.lwd.ferrysupport.android7.fileprovider/files/20171106-165706.png                            
/com.bsoft.lwd.ferrysupport E/files-path: ---------------------------------------------------------                                                       
/com.bsoft.lwd.ferrysupport E/cache-path: /data/user/0/com.bsoft.lwd.ferrysupport/cache/20171106-165706.png                                               
/com.bsoft.lwd.ferrysupport E/cache-path: content://com.bsoft.lwd.ferrysupport.android7.fileprovider/cache/20171106-165706.png                            
/com.bsoft.lwd.ferrysupport E/cache-path: ---------------------------------------------------------                                                       
/com.bsoft.lwd.ferrysupport E/external-path: /storage/emulated/0/20171106-165706.png                                                                      
/com.bsoft.lwd.ferrysupport E/external-path: content://com.bsoft.lwd.ferrysupport.android7.fileprovider/external/20171106-165706.png                      
/com.bsoft.lwd.ferrysupport E/external-path: ---------------------------------------------------------                                                    
/com.bsoft.lwd.ferrysupport E/external-files-path: /storage/emulated/0/Android/data/com.bsoft.lwd.ferrysupport/files/Pictures/20171106-165706.png         
/com.bsoft.lwd.ferrysupport E/external-files-path: content://com.bsoft.lwd.ferrysupport.android7.fileprovider/externalfiles/Pictures/20171106-165706.png  
/com.bsoft.lwd.ferrysupport E/external-files-path: ---------------------------------------------------------                                              
/com.bsoft.lwd.ferrysupport E/external-cache-path: /storage/emulated/0/Android/data/com.bsoft.lwd.ferrysupport/cache/20171106-165706.png                  
/com.bsoft.lwd.ferrysupport E/external-cache-path: content://com.bsoft.lwd.ferrysupport.android7.fileprovider/externalcache/20171106-165706.png           
/com.bsoft.lwd.ferrysupport E/external-cache-path: ---------------------------------------------------------                                                                                                                                                              

你可能感兴趣的:(Android《7.0适配》)