Loading All the Images from Gallery into the Application in Android

Loading all images & videos from gallery in the app activity & selecting some of them

hi I can understand your problem what you want is an image/Video picker library .its bad idea to build it on your own as a beginner so my recommendation would be to choose community build libraries which will make your job much easier

link of a few Image Picker libraries
https://android-arsenal.com/tag/157 you can google to find more libraries

follow the instruction from their Github page and try to implement them in your project it will be much easier

my recommendation would be to use this library https://github.com/Mindinventory/Lassi-Android
as this library provides both image and video to be picked

How to get all the image from gallery to my application using recyclerview?and how to set image in onbind holder for loding gallery images

You have to first get all images from gallery in you activity then have to set the list of images to RecyclerView

Use below method to get all all images-

    private ArrayList<String> getAllShownImagesPath(Activity activity) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList<String> listOfAllImages = new ArrayList<String>();
String absolutePathOfImage = null;
uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

String[] projection = { MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME };

cursor = activity.getContentResolver().query(uri, projection, null,
null, null);

column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_index_folder_name = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);

listOfAllImages.add(absolutePathOfImage);
}
return listOfAllImages;
}

Now set the This list of images to RecyclerView Adapter.

Take this RecyclerView Example as an reffrence to set all gallery images.

How to Load all the media from gallery of phone to the recycler view in the fragment

To Fetch Images

var imageList: ArrayList<String> = ArrayList()
fun fetchImages(): ArrayList<String> {
val columns = arrayOf(MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID)
val imagecursor: Cursor = requireActivity().managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, ""
)
for (i in 0 until imagecursor.count) {
imagecursor.moveToPosition(i)
val dataColumnIndex =
imagecursor.getColumnIndex(MediaStore.Images.Media.DATA)
imageList.add(imagecursor.getString(dataColumnIndex))
}
return imageList
}

To Fetch Videos

var videoList: ArrayList<String> = ArrayList()
fun fetchVideos: ArrayList<String> {
val columns = arrayOf(MediaStore.Video.Media.DATA,
MediaStore.Video.Media._ID)
val imagecursor: Cursor = requireActivity().managedQuery(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
null, ""
)
for (i in 0 until imagecursor.count) {
imagecursor.moveToPosition(i)
val dataColumnIndex =
imagecursor.getColumnIndex(MediaStore.Video.Media.DATA)
videoList.add(imagecursor.getString(dataColumnIndex))
}
return videoList
}

Just concatenate then shuffle array for all fragment or whatever your scenario

Pass these arrays in your adapter

Glide.with(context).load(list.get(position)).into(holder.img_thumbnail)

Hope it helps :) If you have any confusion feel free to ask.

Loading Images from Gallery into ImageViews Separatly

You defined three request codes, but they all have the same value, hence all three if statements will be executed regardless of your image choice.

Simply assigning different values to your request code variables should fix the problem.

private static int RESULT_LOAD_IMAGE = 1;
private static int RESULT_LOAD_IMAGE2 = 2;
private static int RESULT_LOAD_IMAGE3 = 3;


Related Topics



Leave a reply



Submit