【问题标题】:How the codes work for loading image from Gallery Android代码如何从 Gallery Android 加载图像
【发布时间】:2016-07-27 18:17:51
【问题描述】:

我有用于从图库加载图像的代码,但我真的不明白它是如何工作的。这是代码。

    @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

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

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

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

我不明白这一行。

cursor.moveToFirst();

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

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

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

String picturePath = cursor.getString(columnIndex);

既然 columnIndex 总是为 0,那么如何为不同的图片获取不同的路径呢?

谁能帮我检查一下我的解释是否正确并解释我不明白的那一行?谢谢。

【问题讨论】:

    标签: android image android-cursor


    【解决方案1】:

    1-

    Uri selectedImage = data.getData();
    

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

    2-

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

    当您希望游标读取内容提供程序(通过ContentResolver)时,您需要指定需要从数据库中读取哪些列,并且您需要传递的参数应该是一个字符串数组(是否有您仍然需要传递一个数组的一列或多列)。 MediaStore.Images.Media 是一个数据库契约,其中包含您需要用来与内容提供者对话的常量

    3-

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

    游标用于从内容提供者读取数据。如果您熟悉其他编程语言,这就像从数据库中读取行并且您的结果存储在游标中。当你传递 URI 时,你不需要指定读取哪个数据库,ContentResolver 会帮你找出来(这是使用内容提供者的一个优势)

    4-

    cursor.moveToFirst();
    

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

    5-

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

    您需要知道需要访问哪一列才能读取所需数据(在本例中为文件路径名)。所以你问游标file pathname 的列索引是什么,它会返回你的列索引。当然,在这种情况下,它将始终为 0,因为您只要求内容提供者返回一列 (file pathname),因此除此之外没有更多数据可显示

    6-

    String picturePath = cursor.getString(columnIndex);
    

    最后,这条语句要求光标获取位于索引处的file pathname(在本例中为索引0),所以最后你有你的文件路径。注意,此方法一次只能读取一张图片数据

    【讨论】:

      【解决方案2】:

      游标将查询结果记录存储在行中,并授予许多方法来访问和遍历记录。 int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 也是您的索引始终为 0 的原因

      【讨论】:

        猜你喜欢
        • 2014-01-13
        • 1970-01-01
        • 1970-01-01
        • 2014-09-30
        • 1970-01-01
        • 2012-05-20
        • 2016-08-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多