在奇巧(或新画廊)之前,意图。ACTION_GET_CONTENT返回一个这样的URI

内容:/ /媒体/外部/图片/媒体/ 3951。

使用ContentResolver并查询 media . data返回文件URL。

然而,在奇巧,画廊返回一个URI(通过“Last”)像这样:

内容:/ / com.android.providers.media.documents /文档/图片:3951

我该怎么处理呢?


当前回答

如果有人感兴趣,我为ACTION_GET_CONTENT做了一个工作的Kotlin版本:

var path: String = uri.path // uri = any content Uri
val databaseUri: Uri
val selection: String?
val selectionArgs: Array<String>?
if ("/document/image:" in path || "/document/image%3A" in path) {
    // files selected from "Documents"
    databaseUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
    selection = "_id=?"
    selectionArgs = arrayOf(DocumentsContract.getDocumentId(uri).split(":")[1])
} else { // files selected from all other sources, especially on Samsung devices
    databaseUri = uri
    selection = null
    selectionArgs = null
}
try {
    val projection = arrayOf(MediaStore.Images.Media.DATA,
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.ORIENTATION,
        MediaStore.Images.Media.DATE_TAKEN) // some example data you can query
    val cursor = context.contentResolver.query(databaseUri,
        projection, selection, selectionArgs, null)
    if (cursor.moveToFirst()) {
        // do whatever you like with the data
    }
    cursor.close()
} catch (e: Exception) {
    Log.e(TAG, e.message, e)
}

其他回答

有同样的问题,尝试上面的解决方案,但虽然它一般工作,由于某种原因,我得到了一些图像的Uri内容提供者的权限拒绝,尽管我有android.permission。MANAGE_DOCUMENTS权限添加正确。

不管怎样,找到了其他的解决方案,这是迫使打开图库,而不是KITKAT文档视图:

// KITKAT

i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, CHOOSE_IMAGE_REQUEST);

然后加载图片:

Uri selectedImageURI = data.getData();
input = c.getContentResolver().openInputStream(selectedImageURI);
                BitmapFactory.decodeStream(input , null, opts);

EDIT

ACTION_OPEN_DOCUMENT可能需要你持久化权限标志等,通常会导致安全异常…

Other solution is to use the ACTION_GET_CONTENT combined with c.getContentResolver().openInputStream(selectedImageURI) which will work both on pre-KK and KK. Kitkat will use new documents view then and this solution will work with all apps like Photos, Gallery, File Explorer, Dropbox, Google Drive etc...) but remember that when using this solution you have to create image in your onActivityResult() and store it on SD Card for example. Recreating this image from saved uri on next app launch would throw Security Exception on content resolver even when you add permission flags as described in Google API docs (that's what happened when I did some testing)

此外,Android开发者API指南建议:

ACTION_OPEN_DOCUMENT is not intended to be a replacement for ACTION_GET_CONTENT. The one you should use depends on the needs of your app: Use ACTION_GET_CONTENT if you want your app to simply read/import data. With this approach, the app imports a copy of the data, such as an image file. Use ACTION_OPEN_DOCUMENT if you want your app to have long term, persistent access to documents owned by a document provider. An example would be a photo-editing app that lets users edit images stored in a document provider.

你的问题的答案是你需要有权限。在manifest.xml文件中输入以下代码:

<uses-sdk  android:minSdkVersion="8"   android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_OWNER_DATA"></uses-permission>
<uses-permission android:name="android.permission.READ_OWNER_DATA"></uses-permission>`

这对我很管用……

这是一个完全的hack,但这是我所做的…

因此,在设置DocumentsProvider时,我注意到样例代码(在getDocIdForFile中,大约在第450行)根据文件相对于指定根的路径(即在第96行中设置mBaseDir的路径)为所选文档生成了唯一的id。

所以URI最终看起来像这样:

内容:/ / com.example.provider /文档/根:路径/ / /文件

正如文档所说,它假设只有一个根(在我的情况下是Environment.getExternalStorageDirectory(),但你可以在其他地方使用…然后它获取文件路径,从根目录开始,并使其成为唯一的ID,前置“root:”。所以我可以通过消除uri.getPath()中的“/document/root:”部分来确定路径,通过这样做来创建一个实际的文件路径:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
// check resultcodes and such, then...
uri = data.getData();
if (uri.getAuthority().equals("com.example.provider"))  {
    String path = Environment.getExternalStorageDirectory(0.toString()
                 .concat("/")
                 .concat(uri.getPath().substring("/document/root:".length())));
    doSomethingWithThePath(path); }
else {
    // another provider (maybe a cloud-based service such as GDrive)
    // created this uri.  So handle it, or don't.  You can allow specific
    // local filesystem providers, filter non-filesystem path results, etc.
}

我知道。这很可耻,但它奏效了。同样,这依赖于您在应用程序中使用自己的文档提供程序来生成文档ID。

(此外,还有一种更好的方法来构建路径,不假设“/”是路径分隔符,等等。但你懂的。)

正如commonware提到的,你不应该假设,你通过ContentResolver得到的流可以转换成文件。

你真正应该做的是从ContentProvider打开InputStream,然后从中创建一个位图。而且它在4.4和更早的版本上也可以工作,不需要反射。

    //cxt -> current context

    InputStream input;
    Bitmap bmp;
    try {
        input = cxt.getContentResolver().openInputStream(fileUri);
        bmp = BitmapFactory.decodeStream(input);
    } catch (FileNotFoundException e1) {

    }

当然,如果你处理大图片,你应该用适当的inSampleSize: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html加载它们。但那是另一个话题了。

这个Android库处理奇巧的情况变化(包括旧版本- 2.1+): https://github.com/iPaulPro/aFileChooser

使用String path = FileUtils。getPath(context, uri)将返回的uri转换为可用于所有OS版本的路径字符串。 更多信息请访问:https://stackoverflow.com/a/20559175/860488