In the res directory create a folder inside add a xml xml file name such as: 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>
Element must contain one or a plurality of sub-elements. These sub-element is used to specify the directory path to share files, you must be one of these elements:
<Files-path>: internal storage application files / directories under the private directory, equivalent to Context.getFilesDir () acquired directory path;
<Cache-path>: internal storage application cache / private directory under the directory is equivalent to Context.getCacheDir (acquired directory path);
<External-path>: external storage root, equivalent to Environment.getExternalStorageDirectory () acquired directory path;
<External-files-path>: external storage application files / directories under the private directory, the directory path is equivalent to Context.getExternalFilesDir (null) acquired;
<External-cache-path>: Application of external storage cache / private directory under the directory, equivalent to Context.getExternalCacheDir ();
Add in the 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+ obtain and display picture albums
method
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)
Install 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)
}