代码如何用于从Gallery Android加载图像 [英] How the codes work for loading image from Gallery Android

查看:170
本文介绍了代码如何用于从Gallery Android加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码适用于从图库加载图片,但我真的不明白它是如何工作的。以下是代码。

I have the codes work for loading image from gallery but I really do not understand how it works. Here are the codes.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { //Browse Gallery is requested

        Uri selectedImage = data.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 picturePath = cursor.getString(columnIndex);
        cursor.close();

        loadImage(picturePath);         //load picture according the path
        image_View.setImageBitmap(pic); //Show the selected picture
    }
}




Uri selectedImage = data.getData();

Uri selectedImage = data.getData();

从意图中获取所选图像的uri

Get the uri of selected image from intent


String [] filePathColumn = {MediaStore.Images.Media.DATA};

String[] filePathColumn = { MediaStore.Images.Media.DATA };

MediaStore.Images.Media.DATA是常量。我不明白为什么不使用String而不是String []

MediaStore.Images.Media.DATA is constant. I do not understand why do not use String instead of String[]


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

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

我不明白这一行。


cursor.moveToFirst();

cursor.moveToFirst();

移至图库中的第一张图片。

Move to the first picture in Gallery.


int columnIndex = cursor.getColumnIndex(filePathColumn [0]);

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

对于这个,无论我选择哪张图片,我总是得0。

For this one, I always get 0 no matter which picture I choose.


String picturePath = cursor.getString(columnIndex);

String picturePath = cursor.getString(columnIndex);

由于columnIndex总是0,那么它如何为不同的图片获得不同的路径?

Since columnIndex always 0, then how can it get different path for different picture?

任何人都可以帮我检查一下我的解释是否正确并解释我不明白的界限吗?谢谢。

Can anyone help me to check whether my explanation is correct and explain the line that I do not understand? Thanks.

推荐答案

1 -

Uri selectedImage = data.getData();

这是您需要通过<之前调用的另一个意图读取数据的语句code> startActivityForResult 方法。在这种情况下,您可能打开一个intent并让用户选择一个图像,然后图像的 URI 将返回给您并使用 getData 阅读。

This is the statement where you need to read the data passed through another intent which you called earlier via startActivityForResult method. In this case probably you open an intent and let the user select an image, then the URI of the image will be returned to you and you use getData to read that.

2 -

String[] filePathColumn = { MediaStore.Images.Media.DATA };

当你希望游标阅读内容提供者的内容时(通过 ContentResolver )您需要指定需要从数据库中读取哪些列,并且您需要传递的参数应该是String数组(无论它是否还有一个或多个列,您仍需要传递数组)。 MediaStore.Images.Media 是一个数据库合约,其中包含您需要用来与内容提供商交谈的常量

When you want cursors to read something a Content Provider (via ContentResolver) you need to specify which columns you need to read from the database, and the argument you need to pass should be an array of String (whether it has one or more columns you still need to pass an array). MediaStore.Images.Media is a Database Contract which contains constants which you need to use to talk to Content Providers

3 -

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

游标用于从内容提供商处读取数据。如果您熟悉其他编程语言,就像从数据库中读取行,并将结果存储在Cursors中。当您传递URI时,您不需要指定要读取的数据库, ContentResolver 将为您找到(这是使用内容提供程序的优势)

Cursors are used for reading data from Content Providers. If you are familiar with other programming languages it is like reading rows from database and your results are stored in Cursors. When you pass URI, you don't need to specify which database to read, ContentResolver will find that out for you (this is an advantage of using content providers)

4-

cursor.moveToFirst();

当您从数据库中读取所需的行时(在这种情况下,您可能刚刚选择了一个图像),你需要将光标移动到指向返回结果的第一个条目(行)

When you read the desired rows from the database (in this case probably you just selected one image), you need to move the cursor to point to the first entry (row) of the returned results

5 -

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

您需要知道需要访问哪一列才能读取所需数据(在本例中为文件路径)名称)。所以你问游标什么是文件路径名的列索引,它会返回列索引。当然在这种情况下它总是0,因为你只要求内容提供者返回一列(文件路径名),这样就不会有更多的数据显示那个

You need to know which column you need to access to read your desired data (in this case file path name). so you ask cursor what is the column index of the file pathname and it will return you the column index. and of course in this case it will be always 0 because you only asked the content provider to return one column (file pathname) so there would be no more data to show other than that

6 -

String picturePath = cursor.getString(columnIndex);

最后这句话要求游标获取文件路径名位于索引处(在这种情况下索引 0 ),所以最后你有你的文件路径。请注意,使用此方法一次只能读取一个图片数据

and finally this statement asks cursor to get the file pathname located at the index (in this case index 0) so at the end you have your path to file. Note that you can read only one picture data at a time with this method

这篇关于代码如何用于从Gallery Android加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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