美文网首页
Android《7.0适配》

Android《7.0适配》

作者: 泅渡者 | 来源:发表于2018-06-07 11:09 被阅读38次

第一步

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.android7.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>

第二部

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path
        name="root"
        path="" />
    <files-path
        name="files"
        path="files" />
    <cache-path
        name="cache"
        path="caches" />
    <external-path
        name="external"
        path="sdfiles" />
    <external-files-path
        name="externalfiles"
        path="sdfiles" />
    <external-cache-path
        name="externalcache"
        path="sdfiles" />
</paths>

第三部

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适配》

      本文链接:https://www.haomeiwen.com/subject/tqilmxtx.html