ACTION_PICK图像选择器返回不同的URI格式 [英] ACTION_PICK image picker returns diffrent uri formats

查看:287
本文介绍了ACTION_PICK图像选择器返回不同的URI格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用图像选择器选择图像,然后将此图像上传到服务器.

I'm using image picker to select a image and then uploading this image to the server .

我的代码在所有Android设备上均能正常运行, 手机.

My code is working perfectly in all android devices except for Mi phones.

For all device Uri returned is of type : content://media/external/images/media/523

For Mi devices Uri returned is of type:file:///storage/emulated/0/DCIM/Camera/IMG_20160912_160415.jpg

Cursor游标= context.getContentResolver().query()返回 如果uri的格式不是content://*

The Cursor cursor = context.getContentResolver().query() returns null if the uri is not in format content://*

 private void pickImage() {

        Intent photoPickerIntent = new Intent(
                Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        photoPickerIntent.setType("image/*");

        startActivityForResult(photoPickerIntent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);



        if (resultCode == RESULT_OK) {
            Uri selectedImageUri = data.getData();
            if (selectedImageUri != null) {
                selectedImagePath = Utils.getImagePath(selectedImageUri, DepositBankWireActivity.this);
                Log.i("uplaod", "selectedImagePath" + selectedImagePath);
}
}

 public static String getImagePath(Uri uri, Context context) {
        Log.i("getImagePath",""+uri+" mime "+getMimeType(uri,context));

            String[] projection = {MediaStore.MediaColumns.DATA,
                    MediaStore.Images.ImageColumns.ORIENTATION};
            Cursor cursor = context.getContentResolver().query(uri, projection, null, null,
                    null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);

    }

是否有实现图片选择器的标准方法,该选择器返回 来自uri的正确图像路径,例如 /storage/emulated/0/Pictures/Screenshots/test.png进行上传.

Is there a standard way to implement image picker which returns the correct image path from uri eg /storage/emulated/0/Pictures/Screenshots/test.png for upload.

推荐答案

不需要MediaStoreDATA列为您提供可以使用的文件系统路径.例如,图像可能位于Android 4.4+上的可移动存储中,MediaStore可以访问,但您不能访问. MediaStore还可以在其索引图像中包含非本地设备的图像,而是来自Google相册之类的服务(因此,有人告诉我).

There is no requirement for the DATA column of MediaStore to give you a filesystem path that you can use. For example, the image might be on removable storage on Android 4.4+, which the MediaStore can access, but you cannot. MediaStore can also have in its index images that are not local to the device, but are from services like Google Photos (so I've been told).

因此,您的第一步是摆脱getImagePath(),因为它不可靠.

So, your first step is to get rid of getImagePath(), as it will be unreliable.

最简单,最高效的解决方案是找到一种无需文件系统访问即可压缩映像并在服务器上[上传]多部分映像"的方法.例如,您可以通过ContentResolver上的openInputStream()访问InputStream,这对于问题中显示的两种Uri类型都适用.找到一些方法来使用该InputStream.

The simplest and most performant solution is to find a way to "compress the images and [do] a multipart image upload on the server" without filesystem access. For example, you have access to an InputStream via openInputStream() on ContentResolver, and that will work for both types of Uri shown in your question. Find some way to do your work using that InputStream.

如果确定不可能,则无论如何都需要获取InputStream并制作内容的本地副本(例如,在getCacheDir()中).然后,使用您的本地副本,完成后将其删除.

If you determine that this is not possible, you will need to get that InputStream anyway and make a local copy of the content (e.g., in getCacheDir()). Then, use your local copy, deleting it when you are done.

这篇关于ACTION_PICK图像选择器返回不同的URI格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆