如何组合图像中的多个Uri

时间:2016-05-13 13:40:59

标签: android android-gallery mediastore

我有这个查询图库图像的功能

public void getImagesFromGallary(Context context) {

        List<Uri> imagesUri = new ArrayList<>();
    List<Uri> imageOrientationUri  = new ArrayList<>();


        Cursor imageCursor = null;
        try {
            final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.ImageColumns.ORIENTATION};
            final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";


            imageCursor = context.getApplicationContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
            while (imageCursor.moveToNext()) {
                Uri uri = Uri.parse(imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)));
                Uri uriOrientation = Uri.parse(imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION)));
                imagesUri.add(uri);
                imageOrientationUri.add(uriOrientation);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (imageCursor != null && !imageCursor.isClosed()) {
                imageCursor.close();
            }
        }
    }

我是否必须有两个Uri列表,例如我有一个

imagesUri list和imageOrientationUri列表。

如果我需要Image中的更多信息,我必须从光标解析另一个Uri吗?

1 个答案:

答案 0 :(得分:1)

您可以为图像创建对象,如:

public class ImageDetails {

    private URI imageURI;
    private URI imageOrientationURI;

    public URI getImageURI() {
        return imageURI;
    }

    public void setImageURI(URI imageURI) {
        this.imageURI = imageURI;
    }

    public URI getImageOrientationURI() {
        return imageOrientationURI;
    }

    public void setImageOrientationURI(URI imageOrientationURI) {
        this.imageOrientationURI = imageOrientationURI;
    }
}

您创建一个列表并向其添加详细信息,如:

public void getImagesFromGallary(Context context) {

    List<ImageDetails> imageDetails = new ArrayList<>();

    Cursor imageCursor = null;
    try {
        final String[] columns = {MediaStore.Images.Media.DATA, MediaStore.Images.ImageColumns.ORIENTATION};
        final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";

        imageCursor = context.getApplicationContext().getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
        while (imageCursor.moveToNext()) {
            ImageDetails tmp = new ImageDetails();
            tmp.setImageURI(Uri.parse(imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION))));
            tmp.setImageOrientationURI(Uri.parse(imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA))));

            imageDetails.add(tmp);

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (imageCursor != null && !imageCursor.isClosed()) {
            imageCursor.close();
        }
}

因此,对于现在的每个新属性,您可以在类中创建一个新变量以及getter和setter