3 回答

TA貢獻2051條經驗 獲得超10個贊
對于 android Q 從共享存儲訪問媒體文件 為了提供更豐富的用戶體驗,許多應用程序允許用戶貢獻和訪問外部存儲卷上可用的媒體。該框架為媒體集合提供了一個優化的索引,稱為媒體存儲,可以更輕松地檢索和更新這些媒體文件。作用域存儲 android?作用域存儲 android 添加一個項目到集合
public static Uri getImageContentUri(Context context, File imageFile) {
? ? String filePath = imageFile.getAbsolutePath();
? ? Cursor cursor = context.getContentResolver().query(
? ? ? ? ? ? MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
? ? ? ? ? ? new String[]{MediaStore.Images.Media._ID},
? ? ? ? ? ? MediaStore.Images.Media.DATA + "=? ",
? ? ? ? ? ? new String[]{filePath}, null);
? ? if (cursor != null && cursor.moveToFirst()) {
? ? ? ? int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
? ? ? ? cursor.close();
? ? ? ? return Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
? ? } else {
? ? ? ? if (imageFile.exists()) {
? ? ? ? ? ? if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
? ? ? ? ? ? ? ? ContentResolver resolver = context.getContentResolver();
? ? ? ? ? ? ? ? Uri picCollection = MediaStore.Images.Media
? ? ? ? ? ? ? ? ? ? ? ? .getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
? ? ? ? ? ? ? ? ContentValues picDetail = new ContentValues();
? ? ? ? ? ? ? ? picDetail.put(MediaStore.Images.Media.DISPLAY_NAME, imageFile.getName());
? ? ? ? ? ? ? ? picDetail.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
? ? ? ? ? ? ? ? picDetail.put(MediaStore.Images.Media.RELATIVE_PATH,"DCIM/" + UUID.randomUUID().toString());
? ? ? ? ? ? ? ? picDetail.put(MediaStore.Images.Media.IS_PENDING,1);
? ? ? ? ? ? ? ? Uri finaluri = resolver.insert(picCollection, picDetail);
? ? ? ? ? ? ? ? picDetail.clear();
? ? ? ? ? ? ? ? picDetail.put(MediaStore.Images.Media.IS_PENDING, 0);
? ? ? ? ? ? ? ? resolver.update(picCollection, picDetail, null, null);
? ? ? ? ? ? ? ? return finaluri;
? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ContentValues values = new ContentValues();
? ? ? ? ? ? ? ? values.put(MediaStore.Images.Media.DATA, filePath);
? ? ? ? ? ? ? ? return context.getContentResolver().insert(
? ? ? ? ? ? ? ? ? ? ? ? MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
}

TA貢獻1942條經驗 獲得超3個贊
此函數將根據給定的文件顯示名稱返回 Uri。您可以將其更新為 Image 之類的MediaStore.Images....
public static Uri getUriFromDisplayName(Context context, String displayName) {
String[] projection;
projection = new String[]{MediaStore.Files.FileColumns._ID};
// TODO This will break if we have no matching item in the MediaStore.
Cursor cursor = context.getContentResolver().query(extUri, projection,
MediaStore.Files.FileColumns.DISPLAY_NAME + " LIKE ?", new String[]{displayName}, null);
assert cursor != null;
cursor.moveToFirst();
if (cursor.getCount() > 0) {
int columnIndex = cursor.getColumnIndex(projection[0]);
long fileId = cursor.getLong(columnIndex);
cursor.close();
return Uri.parse(extUri.toString() + "/" + fileId);
} else {
return null;
}
}

TA貢獻1836條經驗 獲得超13個贊
該方法Environment#getExternalStoragePublicDirectory(String)已被棄用,以提高用戶隱私。如果您引用了Context(say, ctx),您可以執行以下操作:
val file = File(ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
? ? ? ? ? ? ? ? "meter/lalaland.jpg")
val uri = Uri.fromFile(file)
添加回答
舉報