如何使用MediaStore EXTERNAL_CONTENT_URI

时间:2018-12-02 12:42:59

标签: android android-intent uri

我对两个指向同一图像文件的uri感到困惑。

我从data.getData()获得的有关Intent数据的uri在Picasso上运行良好。调试器对它的标识是{Uri$HierarchicalUri @5962},但是当我尝试使用MediaStoreMedia.DATA获取uri时,我得到了另一个与Picasso不兼容的uri。调试器将其标识为{Uri$StringUri@6079}

如何从HierarchicalUri中提取MediaStore.Images.Media.EXTERNAL_CONTENT_URI

以下是我的代码:

private void openFileChooser() {
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent, PICK_IMAGE_REQUEST);
}//openFileChooser


public Uri getImageUriTest() {
    ContentResolver contentResolver = getContentResolver();
    Uri mediastoreUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Uri resultUri = null;

    Cursor imageCursor = contentResolver.
            query(mediastoreUri, null, null, null, null);

    imageCursor.moveToFirst();

    int indexImageAbsolutePath = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
    String absolutePath = imageCursor.getString(indexImageAbsolutePath);

    resultUri = MediaStore.Images.Media.getContentUri(absolutePath);
    return resultUri;
}//getImageTestUri()


@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
            && data != null && data.getData() != null) {

        imageUri = data.getData();
        imageUriTest = getImageUriTest();

        Picasso.get().load(imageUri).into(imageView);
    }
}//OnActivityResult

0 个答案:

没有答案