Android 4.4年(KitKat)的新 gallery 访问之前,我通过以下方法在SD卡上获得了我的真实路径:

public String getPath(Uri uri) {
   String[] projection = { MediaStore.Images.Media.DATA };
   Cursor cursor = managedQuery(uri, projection, null, null, null);
   startManagingCursor(cursor);
   int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
 return cursor.getString(column_index);
}

现在是意图.操作获取内容返回不同的数据:

之前:

content://media/external/images/media/62

现在:

content://com.android.providers.media.documents/document/image:62

我如何才能获得SD卡上的真实路径?

推荐答案

Note: This answer addresses part of the problem. For a complete solution (in the form of a library), look at 100.

您可以使用URI获取document id,然后查询MediaStore.Images.Media.EXTERNAL_CONTENT_URIMediaStore.Images.Media.INTERNAL_CONTENT_URI(取决于SD卡的情况).

要获取文档id:

// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uriThatYouCurrentlyHave);

// Split at colon, use second item in the array
String id = wholeID.split(":")[1];

String[] column = { MediaStore.Images.Media.DATA };     

// where id is equal to             
String sel = MediaStore.Images.Media._ID + "=?";

Cursor cursor = getContentResolver().
                          query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
                          column, sel, new String[]{ id }, null);

String filePath = "";

int columnIndex = cursor.getColumnIndex(column[0]);

if (cursor.moveToFirst()) {
    filePath = cursor.getString(columnIndex);
}   

cursor.close();

参考:我找不到这个解决方案的来源.我想请原始海报在这里投稿.今晚我会再看看.

Android相关问答推荐

如何解决Android Studio中的in fragment问题

在Kotlin Jetpack Compose中点击按钮后启动另一个Android应用程序

如何将子零部件的大小调整为可以调整大小的父组件大小?

Google Play测试应用程序Crash-java.lang.NoSuchFieldError:没有Lkotlinx/coroutines/CoroutineExceptionHandler类型的字段键

如何在安卓系统上使用Float16霓虹灯?

如何删除Jetpack Compose中的Textfield底线

Android从已连接的设备获得GATT

Lateinit变量结果始终以kotlin格式未初始化

如何在Jetpack Compose中向SearchBar添加边框

升级到 Jetpack Compose 物料 list 2023.08.00 需要我将 targetSdk 更改为 34

有没有办法迭代类型安全的项目访问器?

Android 应用程序从 Android Studio 安装,但不是作为 .apk 在外部安装.抛出java.lang.UnsatisfiedLinkError

Jetpack Compose 如何使一个项目相对于另一个项目垂直居中

ArrayList 上的 Android intent.getParcelableArrayListExtra 引发 Nullpointer 异常

在 Jetpack Compose 中自动滚动后面的项目

如何在 BasicTextField 中全选焦点

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

IconButton 中可绘制的图标是黑色的,尽管它是白色的

禁用通知权限后启动前台服务导致崩溃(Android 13)

如何根据加载图像的 colored颜色 绘制边框?