I am creating an application wherein I need all the images in the gallery into my application that has a girdview in it. I want all the images from all the folders to appear in the gridview.
String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; actualimagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null, null, MediaStore.Images.Media.DEFAULT_SORT_ORDER); actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID); actualimagecursor.moveToPosition(position); String i = actualimagecursor.getString(actual_image_column_index);
I have added this code into my code but i get only the sd card images no other folder images are obtained. How can i get all the images from the gallery?
Thanks in advance.
You can use below code to get all images from specific folder. 1) First you need to define File object to get the storage and appened the name of the folder you want to read. File folder = new File(Environment. getExternalStorageDirectory().
Working solution with the help of Glide. Bonus part is Glide will auto play Gif .
import java.util.ArrayList; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.provider.MediaStore.MediaColumns; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; import android.widget.Toast; import com.bumptech.glide.Glide; /** * The Class GallarySample. */ public class GallarySample extends Activity { /** The images. */ private ArrayList<String> images; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gallery_activity); GridView gallery = (GridView) findViewById(R.id.galleryGridView); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { if (null != images && !images.isEmpty()) Toast.makeText( getApplicationContext(), "position " + position + " " + images.get(position), 300).show(); ; } }); } /** * The Class ImageAdapter. */ private class ImageAdapter extends BaseAdapter { /** The context. */ private Activity context; /** * Instantiates a new image adapter. * * @param localContext * the local context */ public ImageAdapter(Activity localContext) { context = localContext; images = getAllShownImagesPath(context); } public int getCount() { return images.size(); } public Object getItem(int position) { return position; } public long getItemId(int position) { return position; } public View getView(final int position, View convertView, ViewGroup parent) { ImageView picturesView; if (convertView == null) { picturesView = new ImageView(context); picturesView.setScaleType(ImageView.ScaleType.FIT_CENTER); picturesView .setLayoutParams(new GridView.LayoutParams(270, 270)); } else { picturesView = (ImageView) convertView; } Glide.with(context).load(images.get(position)) .placeholder(R.drawable.ic_launcher).centerCrop() .into(picturesView); return picturesView; } /** * Getting All Images Path. * * @param activity * the activity * @return ArrayList with images Path */ 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; } } }
Layout file for gridView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <GridView android:id="@+id/galleryGridView" android:layout_width="match_parent" android:layout_height="wrap_content" android:columnWidth="280dp" android:gravity="center" android:horizontalSpacing="2dp" android:numColumns="2" android:padding="2dp" android:stretchMode="columnWidth" android:verticalSpacing="2dp" > </GridView> </RelativeLayout>
Kotlin
and LoderManager
:** 2) Use LoderManager to load async load images.**
Implement LoaderManager.LoaderCallbacks<Cursor>
in your activity or Fragment class
Override LoaderCallbacks this:
private val IMAGE_LOADER_ID = 1 private val listOfAllImages = ArrayList<String>() override fun onCreateLoader(p0: Int, p1: Bundle?): Loader<Cursor> { val uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI val projection = arrayOf(MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME) val selection: String? = null //Selection criteria val selectionArgs = arrayOf<String>() //Selection criteria val sortOrder: String? = null return CursorLoader( activity!!.applicationContext, uri, projection, selection, selectionArgs, sortOrder) } override fun onLoadFinished(loader: Loader<Cursor>, cursor: Cursor?) { cursor?.let { val columnIndexData = it.getColumnIndexOrThrow(MediaColumns.DATA); while (it.moveToNext()) { listOfAllImages.add(it.getString(columnIndexData)); } } } override fun onLoaderReset(loader: Loader<Cursor>) { }
Lastly on onCreate method init loader:
loaderManager.initLoader(IMAGE_LOADER_ID, null, this)
Kotlin Coroutine
& ViewModel
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-M2'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0-M2"
Create View Model With Coroutine
Context
/** * Use Coroutines To Load Images */ class ImageViewModel : ViewModel(), CoroutineScope { private val job = Job() override val coroutineContext: CoroutineContext get() = job + Dispatchers.Main private var imagesLiveData: MutableLiveData<List<String>> = MutableLiveData() fun getImageList(): MutableLiveData<List<String>> { return imagesLiveData } /** * Getting All Images Path. * * Required Storage Permission * * @return ArrayList with images Path */ internal fun loadImagesfromSDCard(): ArrayList<String> { val uri: Uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI val cursor: Cursor? val column_index_data: Int val column_index_folder_name: Int val listOfAllImages = ArrayList<String>() var absolutePathOfImage: String? = null val projection = arrayOf(MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME) cursor = AppController.globalContentResolvere!!.query(uri, projection, null, null, null) column_index_data = cursor!!.getColumnIndexOrThrow(MediaStore.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 } fun getAllImages() { launch(Dispatchers.Main) { imagesLiveData.value = withContext(Dispatchers.IO) { loadImagesfromSDCard() } } } }
And then use viewModel
val imageViewModel = ViewModelProviders.of(this).get(ImageViewModel::class.java) imageViewModel.getImageList().observe(this, Observer<List<String>> { listOfImage -> imageWidgetStatus.text = """ Found ${listOfImage.size} Images""" // load images imageViewModel.getAllImages()
You are using MediaStore.Images.Media.EXTERNAL_CONTENT_URI
which is only the external storage.For the internal there is MediaStore.Images.Media.INTERNAL_CONTENT_URI
. You can use a MergeCursor to combine both query results.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With