0

I'm trying to build an image gallery like the android native image gallery. I'm using an CursorAdapter, with a disk cache and Mem cache and i adjust all the images to the minimum size, Also tried the inBitamp option of the BitmapFactory, Nothing seem to make the loading fast and smooth enough like the regular Gallery.

This is the Runnable which mainly does all the loading.

    /**
 * Class to asynchronously perform the loading of the bitmap
 */
public class ImageCalcInSampleRunnable implements Runnable {
    protected ImageLoadingArgs mArgs = null;

    public ImageLoadingArgs getmArgs() {
        return mArgs;
    }


    public ImageCalcInSampleRunnable(ImageLoadingArgs args) {
        mArgs = args;
    }

    Future mMyTask = null;

    long timeCounter = 0;


    public void run() {
        timeCounter = System.currentTimeMillis();

        if (mArgs.position < mFirstVisibleItem - IMAGE_CANCEL_THERSHOLD
                ||
                mArgs.position > mLastVisibleCount + IMAGE_CANCEL_THERSHOLD) {

            if (firstReturn) {

                firstReturn = false;
            }
            return;
        }

        firstReturn = true;
        boolean ismimeTypeImage = true;
        if (mArgs.mMimeTpe != null && mArgs.mMimeTpe.contains("video")) {
            ismimeTypeImage = false;
        }

        if (mDiskCache.containsKey(mArgs.mImageId)) {


            mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);

            addBitmapToMemoryCache(ContentUris.parseId(mArgs.mUri), mArgs.mBitmap);
            if (mArgs.mBitmap != null)

                if (!ismimeTypeImage)
                    mArgs.isVideo = true;



        } else {


            if (ismimeTypeImage) {
                try {
                    mArgs.mBitmap = MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, Long.parseLong(mArgs.mImageId),
                            MediaStore.Images.Thumbnails.MINI_KIND, null);

                    mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, false);

                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

                if (mArgs.mBitmap == null) {
                    mArgs.options = ImageManager.decodeInSampledBitmapFromResourceMulti(mArgs.mData, mArgs.mWidth, mArgs.mheight);
                    if (Utils.hasHoneycomb()) {
                        mDiskCache.addInBitmapOptions(mArgs.options);

                        mArgs.mBitmap = BitmapFactory.decodeFile(mArgs.mData, mArgs.options);
                    }else {
                        mArgs.mBitmap = BitmapFactory.decodeFile(mArgs.mData, mArgs.options);
                    }

                    mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, false);

                }

                if (mArgs.mBitmap != null) {

                    AddBitmapToDiskCache(mArgs.mImageId, mArgs.mBitmap);

                    if (mDiskCache.containsKey(mArgs.mImageId)) {
                        mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);
                    }

                        addBitmapToMemoryCache(ContentUris.parseId(mArgs.mUri), mArgs.mBitmap);
                } else {
                    try {
                        mArgs.mBitmap = MediaStore.Video.Thumbnails.getThumbnail(mContentResolver, Long.parseLong(mArgs.mImageId),
                                MediaStore.Video.Thumbnails.MINI_KIND, null);
                        mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, true);

                    } catch (NumberFormatException e) {
                    }
                    if (mArgs.mBitmap != null) {
                        mArgs.isVideo = true;
                        AddBitmapToDiskCache(mArgs.mImageId, mArgs.mBitmap);
                        if (mDiskCache.containsKey(mArgs.mImageId)) {
                            mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);
                        }
                        addBitmapToMemoryCache(Long.parseLong(mArgs.mImageId), mArgs.mBitmap);
                    }
                }


            } else {
                try {
                    mArgs.mBitmap = MediaStore.Video.Thumbnails.getThumbnail(mContentResolver, Long.parseLong(mArgs.mImageId),
                            MediaStore.Video.Thumbnails.MINI_KIND, null);
                    mArgs.mBitmap = scaleCenterCrop(mArgs.mBitmap, mImageWidth, mImageWidth, true);

                } catch (NumberFormatException e) {
                }
                if (mArgs.mBitmap != null) {
                    mArgs.isVideo = true;
                    AddBitmapToDiskCache(mArgs.mImageId, mArgs.mBitmap);
                    if (mDiskCache.containsKey(mArgs.mImageId)) {
                        mArgs.mBitmap = mDiskCache.getBitmap(mArgs.mImageId);
                    }
                    addBitmapToMemoryCache(Long.parseLong(mArgs.mImageId), mArgs.mBitmap);
                }
            }
        }



        if (mArgs.position > mFirstVisibleItem - IMAGE_CANCEL_THERSHOLD
                &&
                mArgs.position < mLastVisibleCount + IMAGE_CANCEL_THERSHOLD) {

            mArgs.mHandler.sendMessage(mArgs.mHandler.obtainMessage(1,mArgs));

        }

        Log.d("shimi5", "timecount=" + (System.currentTimeMillis() - timeCounter));
        timeCounter = (System.currentTimeMillis() - timeCounter);
        if (timeCounter < 45) {
            try {
                Thread.sleep(45 - timeCounter);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }


    }

    public void cancel() {

    }

    @Override
    public boolean equals(Object obj) {
        if (obj != null && obj instanceof ImageCalcInSampleRunnable) {
            if (mArgs.mUri != null) {
                return mArgs.mUri.equals(((ImageCalcInSampleRunnable) obj).mArgs);
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return mArgs.mUri.hashCode();
    }

}

After this a handler simply does setImageBitmap to the image view. Any help with be very helpful

Thanks.

1 Answer 1

1

After a while on this problem i gave up trying to optimize my code for a cleaner and smoother loading of images.

I found this : https://github.com/georgiecasey/android-gallery-library

This is a fork of the Android open source Gallery ICS.

Found allot of insight in this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.