How to make bitmap loading faster in Android?

Viewed 533

I have a code that loads the thumbnail of the image and feeds it to glide. Then glide will load in the holder. but, As I am loading the thumbnail it returns a bitmap. SO, I am passing bitmap to glide. But, this takes nearly 30sec for initial loading. I mean in the gallery there are lots of photos. So, when the gallery is opened it won't show images for 30sec at all. And then it will show the bitmaps.

on the other hand, if I use URI to feed to glide it will load it immediately. But, Images are being loaded in recycler view. So, only some images will be loaded at the time.

After some images are loaded. When we swipe up and see other photos loading speed then obviously bitmap fed images are loaded way faster than URI fed images. But, bitmap takes 30sec of initial loading time.

Is there any way to make this bitmap load faster?

...
while (cursor.moveToNext()) {
                Log.d("FetchImages(): ", " Started");
                Bitmap thumbBitmap = null;

                int _thumpId = cursor.getInt(column_index_data);
                Uri uri1 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, _thumpId);

                Size thumbSize = new Size(150, 150);
                try {
                    thumbBitmap = getApplicationContext().getContentResolver().loadThumbnail(uri1, thumbSize, null);
                } catch (IOException e) {e.printStackTrace();}

                ImageModel ImageModel = new ImageModel();
                ImageModel.setBitmap(thumbBitmap);
                arrayList.add(ImageModel);
            }
            Log.d("FetchImages(): ", " Ended");
            runOnUiThread(() -> {
                Adapter Adapter = new Adapter(getApplicationContext(), arrayList, MainActivity.this);
                recyclerView.setAdapter(Adapter);
                cursor.close();
                Log.d("FetchImages(): ", " RecyclerView Adapter attached");
            });
...
1 Answers

Glide .with(context) .load(url).apply(new RequestOptions().override(150, 150)) //for resizing .into(imageView);

No need to create bitmap it takes more computation . Glide is more faster and efficient. And still if you want to use bitmap then use AsycTask so it wont block main thread and program will not hang.

Related