Loading all the images from gallery into the Application in android

前端 未结 6 1155
离开以前
离开以前 2020-11-29 18:10

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

6条回答
  •  孤独总比滥情好
    2020-11-29 18:35

    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 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 getAllShownImagesPath(Activity activity) {
                Uri uri;
                Cursor cursor;
                int column_index_data, column_index_folder_name;
                ArrayList listOfAllImages = new ArrayList();
                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

    
    
        
        
    
    
    

    UPDATE 2019 with Kotlin and LoderManager:

    ** 2) Use LoderManager to load async load images.**

    Implement LoaderManager.LoaderCallbacks in your activity or Fragment class

    Override LoaderCallbacks this:

    private val IMAGE_LOADER_ID = 1
    private val listOfAllImages = ArrayList()
    
    
    
    override fun onCreateLoader(p0: Int, p1: Bundle?): Loader {
    
        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()  //Selection criteria
        val sortOrder: String? = null
    
        return CursorLoader(
                activity!!.applicationContext,
                uri,
                projection,
                selection,
                selectionArgs,
                sortOrder)
    }
    
    override fun onLoadFinished(loader: Loader, cursor: Cursor?) {
    
        cursor?.let {
            val columnIndexData = it.getColumnIndexOrThrow(MediaColumns.DATA);
    
            while (it.moveToNext()) {
    
                listOfAllImages.add(it.getString(columnIndexData));
            }
        }
    }
    
    
    override fun onLoaderReset(loader: Loader) {
    }
    

    Lastly on onCreate method init loader:

     loaderManager.initLoader(IMAGE_LOADER_ID,
                    null,
                    this)
    

    3) Using 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> = MutableLiveData()
        fun getImageList(): MutableLiveData> {
            return imagesLiveData
        }
    
        /**
         * Getting All Images Path.
         *
         * Required Storage Permission
         *
         * @return ArrayList with images Path
         */
        internal fun loadImagesfromSDCard(): ArrayList {
            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()
            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> { listOfImage ->
            imageWidgetStatus.text = """ Found ${listOfImage.size} Images"""
    
    
        // load images
        imageViewModel.getAllImages()
    

提交回复
热议问题