FileProvider配置

//:/data/user/0/com.example.fileproviderdemo/cache
                Log.i("wanlijun","getCacheDir:"+getCacheDir().toString());
                //:/data/user/0/com.example.fileproviderdemo/files
                Log.i("wanlijun","getFilesDir:"+getFilesDir().toString());
                //:/storage/emulated/0/Android/data/com.example.fileproviderdemo/cache
                //手机上的/Android/data/com.example.fileproviderdemo/cache目录
                Log.i("wanlijun","getExternalCacheDir:"+getExternalCacheDir().toString());
                //:/storage/emulated/0/Android/data/com.example.fileproviderdemo/files/text/plain
                //手机上的/Android/data/com.example.fileproviderdemo/files/text/plain目录
                Log.i("wanlijun","getExternalFilesDir:"+getExternalFilesDir("text/plain").toString());
                //:/storage/emulated/0
                //手机上内部存储设备的根目录
                Log.i("wanlijun","getExternalStorageDirectory:"+ Environment.getExternalStorageDirectory().toString());
                //:/data
                Log.i("wanlijun","getDataDirectory:"+Environment.getDataDirectory().toString());
                //:/data/cache
                Log.i("wanlijun","getDownloadCacheDirectory:"+Environment.getDownloadCacheDirectory().toString());
                //:/storage/emulated/0/text/plain
                Log.i("wanlijun","getExternalStoragePublicDirectory:"+Environment.getExternalStoragePublicDirectory("text/plain").toString());
                File file = new File(getExternalFilesDir("text/plain"),"a.txt");

//                File file = new File(Environment.getExternalStorageDirectory()+"/zhihu","a.txt");
                Log.i("wanlijun",file.getAbsolutePath());
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_VIEW);
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
                    Uri uri = FileProvider.getUriForFile(MainActivity.this,getPackageName()+".provider",file);
                    Log.i("wanlijun",uri.toString());
                    intent.setDataAndType(uri,"text/plain");
                    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }else {
                    intent.setDataAndType(Uri.fromFile(file), "text/plain");
                }
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

AndroidManifest.xml配置:

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

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <!--举个例子解释一下(external-files-path)-->
    <!--代表目录:/storage/emulated/0/Android/data/com.example.fileproviderdemo/files/text/plain-->
    <!--external-files-path就代表/storage/emulated/0/Android/data/com.example.fileproviderdemo/files/text/plain-->
    <!--后面的path就是替换content://com.example.fileproviderdemo.provider/fileFile/text/plain/a.txt这里的fileFile的-->
    <!--只要两个目录最后一样就可以了-->
    <!--/storage/emulated/0/Android/data/com.example.fileproviderdemo/files/text/plain/a.txt-->
    <!--content://com.example.fileproviderdemo.provider/fileFile/text/plain/a.txt-->
    <external-path name="myFileProvider" path="zhihu/"/>
    <external-cache-path name="cacheFile" path="/"/>
    <external-files-path name="fileFile" path="/"/>
</paths>

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页