[英]Get file name from Uri in Android
我有一個 Uri 使用下面的代碼構建
final Uri fileUri = DocumentsContract.buildChildDocumentsUriUsingTree(rootUri, docId);
包含值:
content://com.android.externalstorage.documents/tree/primary%Sample/document/primary%Sample%2FMedia%2F.Hide%2FScreenshot_1615959401.png/children
下面是我訪問文件名稱和 mime 類型的代碼:
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(fileUri, new String[]{DocumentsContract.Document.COLUMN_DOCUMENT_ID, DocumentsContract.Document.COLUMN_DISPLAY_NAME, DocumentsContract.Document.COLUMN_MIME_TYPE}, null, null, null);
try {
if (cursor != null && cursor.moveToFirst())
{
//Here cursor.moveToFirst() returns false
}
} catch(Exception e)
{
e.printStackTrace();
}
我想從 Uri 上方訪問像 Screenshot_1615959401.png 這樣的文件名,像 image/png 這樣的 mime 類型文件,但是 cursor.moveToFirst() 返回 0。為什么即使 Uri 有效也找不到記錄。 因為我嘗試使用此 Uri 加載圖像,並且 ImageView 使用上面的 Uri 正確顯示圖像。
任何幫助都將受到高度贊賞,因為我花了很多時間谷歌搜索和閱讀 SO,但沒有找到任何解決方案或根本原因。
下面是我訪問文件名稱和 mime 類型的代碼:
該Uri
不適用於文檔。 它用於文檔樹的子文檔名冊,其中樹由docId
標識。 childDocumentsUri
是您正在調用的方法名稱的核心。
為什么即使 Uri 有效也找不到記錄
它對您想要的操作無效。
如果您希望為文檔構建Uri
,給定文檔 ID 和樹, 請使用buildDocumentUriUsingTree()
,而不是buildChildDocumentsUriUsingTree()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.