import gallery images which are stored in phone/memory card into recyclerview

二次信任 提交于 2019-12-12 05:34:51

问题


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.



来源:https://stackoverflow.com/questions/37154915/import-gallery-images-which-are-stored-in-phone-memory-card-into-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!