import gallery images which are stored in phone/me

2019-09-02 06:39发布

问题:

I am trying to populate recyclerview from gallery images as instagram imports gallery images while you select images from the list to upload. I want to implement similar type of thing. Any suggestions on how to access the gallery images on phone/memory card and populate the recyclerview. Also please do suggest any way to implement instagram like interface for selecting images to upload.

回答1:

you can use Android media store to pick pictures,Videos and Audios

example code for images:

    private void imageMediaQuery() {
    String[] projection = new String[]{
            MediaStore.MediaColumns.DATA,
            //MediaStore.Images.Media._ID,
            MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
            MediaStore.Images.Media.DATE_TAKEN
    };

    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    String BUCKET_GROUP_BY =
            "1) GROUP BY 1,(1";
    String BUCKET_ORDER_BY = MediaStore.Images.Media.DATE_TAKEN + " DESC";

    Cursor cur = this.getContentResolver().query(images,
            projection, // Which columns to return
            BUCKET_GROUP_BY,       // Which rows to return (all rows)
            null,       // Selection arguments (none)
            BUCKET_ORDER_BY        // Ordering
    );

    if(cur!=null){
        Log.i("ListingImages", " query count=" + cur.getCount());
    }


    if (cur!=null&&cur.moveToFirst()) {
        String bucket;
        String path;
        int bucketColumn = cur.getColumnIndex(
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

        int pathColumn = cur.getColumnIndex(
                MediaStore.MediaColumns.DATA);

        do {
            bucket = cur.getString(bucketColumn);
            path = cur.getString(pathColumn);

        } while (cur.moveToNext());
        cur.close();

    }
}

In the above code you get path for each image and simply use that path in your onBindViewHolder of recyclerview adapter to display images.hope this helps.