我想获取Android Q中文件位置的Uri。我正在使用以下代码,但是,Android Studio声明已弃用。如何正确获取文件的Uri?
val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/meter"), "lalaland.jpg")
val uri = Uri.fromFile(file)
答案 0 :(得分:0)
此功能将根据给定的displayName 返回文件的Uri。您可以将其更新为 MediaStore.Images ....
之类的Image。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;
}
}
答案 1 :(得分:0)
不建议使用方法Environment#getExternalStoragePublicDirectory(String)
,以改善用户隐私。如果您引用了Context
(例如ctx
),则可以执行以下操作:
val file = File(ctx.getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"meter/lalaland.jpg")
val uri = Uri.fromFile(file)
请咨询the docs,以详细了解getExternalFilesDir()
的工作方式。
答案 2 :(得分:0)
用于来自共享存储的android Q Access媒体文件 为了提供更丰富的用户体验,许多应用程序允许用户贡献和访问外部存储卷上可用的媒体。该框架为媒体集合(称为媒体商店)提供了优化的索引,该索引可以更轻松地检索和更新这些媒体文件。作用域存储android scoped storage android add an item to collection
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;
}
}
}