如何挑选画廊(SD卡),我的应用程序的形象呢? [英] How to pick an image from gallery (SD Card) for my app?

查看:198
本文介绍了如何挑选画廊(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 gallery and I would be able to select an image. The selected image would appear in my ImageView.

推荐答案

编辑近5年后,这个最初写:

Edit nearly 5 years after this was originally written:

下面不再是code工作可靠,从各种来源的影像有时会返回一个不同的内容URI,即内容:// ,而不是文件:// 。下方的比我的code更好的解决方案是简单地使用 context.getContentResolver()。openInputStream(intent.getData()),因为这会返回一个InputStream的你可以处理任您选择。例如, BitmapFactory.de codeInputStream()完全在这种情况下,你还可以再使用的选项和inSampleSize场下采样大图像。

The code below no longer works reliably, as images from various sources sometimes return with a different content URI, i.e. content:// rather than file://. A much better solution than my code below is to simply use context.getContentResolver().openInputStream(intent.getData()), as that will return an InputStream that you can handle as you choose. For example, BitmapFactory.decodeInputStream() works perfectly in this situation, as you can also then use the Options and inSampleSize field to downsample large images.

有一点需要注意的:像谷歌驱动器返回URI来已没有实际尚未下载图像。因此,你需要在后台线程执行getContentResolver()code。

One thing to be aware of: 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.

其他答案解释如何发送的意图,但他们并没有解释清楚如何处理响应。下面是关于如何做到这一点的一些示例code:

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做任何你想要与选定的图像。此code工作通过获取在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 code。简单地获得根据它的名称,同一个在滤波处理中使用的列的数目。一旦你明白了,你终于可以去$ C C图像$成与code我给的最后一行的位图。

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天全站免登陆