android -------- android 7.0 + FileProvider 訪問隱私文件和安裝應用的適配

在 res 目錄下新建一個 xml 文件夾 裏邊添加一個xml文件 名字比如:file_paths.xml

<?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="." />
    <cache-path
        name="cache"
        path="/"/>
    <external-path
        name="external"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <external-files-path
        name="external_file"
        path="." />

</paths>

 

元素必須包含一到多個子元素。這些子元素用於指定共享文件的目錄路徑,必須是這些元素之一:

<files-path>:內部存儲空間應用私有目錄下的 files/ 目錄,等同於 Context.getFilesDir() 所獲取的目錄路徑;

<cache-path>:內部存儲空間應用私有目錄下的 cache/ 目錄,等同於 Context.getCacheDir() 所獲取的目錄路徑;

<external-path>:外部存儲空間根目錄,等同於 Environment.getExternalStorageDirectory() 所獲取的目錄路徑;

<external-files-path>:外部存儲空間應用私有目錄下的 files/ 目錄,等同於 Context.getExternalFilesDir(null) 所獲取的目錄路徑;

<external-cache-path>:外部存儲空間應用私有目錄下的 cache/ 目錄,等同於 Context.getExternalCacheDir();

 

在application下添加

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

Android 7.0+獲取相冊圖片並顯示

方法

 public static Uri getImageContentUri(Context context, String path) {
        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ",
                new String[]{path}, null);
        if (cursor != null && cursor.moveToFirst()) {
            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
            Uri baseUri = Uri.parse("content://media/external/images/media");
            return Uri.withAppendedPath(baseUri, "" + id);
        } else {
            // 如果圖片不在手機的共享圖片數據庫,就先把它插入。
            if (new File(path).exists()) {
                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.DATA, path);
                return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
            } else {
                return null;
            }
        }
    }

var urll = UtilImags.getImageContentUri(this, picturePath)
// Glide使用uri加載圖片
// Glide.with(this).load(urll).into(iv_logo)


val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(urll))
iv_logo.setImageBitmap(bitmap)

 

安裝apk

    /***
     * 安裝apk
     */
    fun showInstallApp(fileSavePath: String) {
        var intent = Intent(Intent.ACTION_VIEW)
        var apkFile = File(fileSavePath)
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            var uri = FileProvider.getUriForFile(videoNewsFragment.context!!, videoNewsFragment.context!!.getPackageName() + ".fileprovider", apkFile)
            intent.setDataAndType(uri, "application/vnd.android.package-archive")
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive")
        }
        videoNewsFragment.startActivity(intent)
    }

 

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