如何为我的应用从图库(SD 卡)中选择图像? [英] How to pick an image from gallery (SD Card) for my app?

查看:25
本文介绍了如何为我的应用从图库(SD 卡)中选择图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题最初是针对 Android 1.6 提出的.

This question was originally asked for Android 1.6.

我正在我的应用中处理照片选项.

I am working on photos options in my app.

我的活动中有一个按钮和一个 ImageView.当我单击按钮时,它会重定向到图库,我将能够选择图像.所选图像将出现在我的 ImageView 中.

I have a button and an ImageView in my Activity. When I click the button it would redirect to the gallery and I would be able to select an image. The selected image would appear in my ImageView.

推荐答案

更新的答案,将近 5 年后:

Updated answer, nearly 5 years later:

原始答案中的代码不再可靠,因为来自不同来源的图像有时会返回不同的内容 URI,即 content:// 而不是 file://.更好的解决方案是简单地使用 context.getContentResolver().openInputStream(intent.getData()),因为这将返回一个您可以选择处理的 InputStream.

The code in the original answer no longer works reliably, as images from various sources sometimes return with a different content URI, i.e. content:// rather than file://. A better solution is to simply use context.getContentResolver().openInputStream(intent.getData()), as that will return an InputStream that you can handle as you choose.

例如,BitmapFactory.decodeStream() 在这种情况下效果很好,因为您还可以使用 Options 和 inSampleSize 字段对大图像进行下采样并避免内存问题.

For example, BitmapFactory.decodeStream() works perfectly in this situation, as you can also then use the Options and inSampleSize field to downsample large images and avoid memory problems.

但是,诸如 Google Drive 之类的东西会将 URI 返回到尚未实际下载的图像.因此,您需要在后台线程上执行 getContentResolver() 代码.

However, things like Google Drive return URIs to images which have not actually been downloaded yet. Therefore you need to perform the getContentResolver() code on a background thread.

原答案:

其他答案解释了如何发送意图,但他们没有很好地解释如何处理响应.以下是有关如何执行此操作的一些示例代码:

The other answers explained how to send the intent, but they didn't explain well how to handle the response. Here's some sample code on how to do that:

protected void onActivityResult(int requestCode, int resultCode, 
       Intent imageReturnedIntent) {
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = imageReturnedIntent.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(
                               selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();


            Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
        }
    }
}

在此之后,您已将所选图像存储在yourSelectedImage"中,您可以随意使用.此代码通过在 ContentResolver 数据库中获取图像的位置来工作,但仅靠它是不够的.每张图片大约有 18 列信息,范围从其文件路径到上次修改日期",再到照片拍摄地点的 GPS 坐标,尽管许多字段实际上并未使用.

After this, you've got the selected image stored in "yourSelectedImage" to do whatever you want with. This code works by getting the location of the image in the ContentResolver database, but that on its own isn't enough. Each image has about 18 columns of information, ranging from its filepath to 'date last modified' to the GPS coordinates of where the photo was taken, though many of the fields aren't actually used.

为了节省时间,因为您实际上并不需要其他字段,光标搜索是通过过滤器完成的.过滤器的工作方式是指定所需列的名称 MediaStore.Images.Media.DATA,即路径,然后将该字符串 [] 提供给游标查询.游标查询返回路径,但在使用 columnIndex 代码之前,您不知道它在哪一列.这只是根据名称获取列的编号,与过滤过程中使用的名称相同.完成后,您终于可以使用我提供的最后一行代码将图像解码为位图.

To save time as you don't actually need the other fields, cursor search is done with a filter. The filter works by specifying the name of the column you want, MediaStore.Images.Media.DATA, which is the path, and then giving that string[] to the cursor query. The cursor query returns with the path, but you don't know which column it's in until you use the columnIndex code. That simply gets the number of the column based on its name, the same one used in the filtering process. Once you've got that, you're finally able to decode the image into a bitmap with the last line of code I gave.

这篇关于如何为我的应用从图库(SD 卡)中选择图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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