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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章