亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Android Q 中獲取圖片 Uri?

如何在 Android Q 中獲取圖片 Uri?

蝴蝶刀刀 2023-03-31 17:03:09
我想獲取 Android Q 中文件位置的 Uri。我正在使用以下代碼,但是,Android Studio 聲明它已被棄用。如何以正確的方式獲取文件的 Uri?val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/meter"), "lalaland.jpg")val uri = Uri.fromFile(file)
查看完整描述

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;

? ? ? ? }

? ? }

}


查看完整回答
反對 回復 2023-03-31
?
手掌心

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;

    }


}


查看完整回答
反對 回復 2023-03-31
?
開心每一天1111

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)


查看完整回答
反對 回復 2023-03-31
  • 3 回答
  • 0 關注
  • 249 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號