uCrop icon indicating copy to clipboard operation
uCrop copied to clipboard

It take long time on first time load image

Open nthieu90 opened this issue 4 years ago • 5 comments

Hi all, I have tried on 2.2.4 or 2.2.4-native but it always happens on the first-time load image. It has loading - more than a minute, blank screen, can not scale or rotate. Thanks for any helping

nthieu90 avatar Jan 20 '20 03:01 nthieu90

Hi @nthieu90, can you provide please more details(size of the photo, device-specific, etc.) about this issue.

Marina24 avatar Jan 22 '20 06:01 Marina24

Hi @Marina24, It happens on many different devices and photos, eg: 960x1028 on Xiaomi Mi A2. If I run for the first time it always takes a long time but on the second it loads immediately.

nthieu90 avatar Jan 23 '20 15:01 nthieu90

I have the same issue

Zweihui avatar Dec 23 '20 07:12 Zweihui

I found the solution. In my case,BitmapLoadTask is not executed immediately.So I guess it may be that the thread pool in AsyncTask is full. This is the code of my solution:

public class BitmapLoadUtils {

    private static final String TAG = "BitmapLoadUtils";
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    public static final Executor UCROP_THREAD_POOL_EXECUTOR;
    private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE_SECONDS = 30;
    private static final BlockingQueue<Runnable> sPoolWorkQueue =
        new LinkedBlockingQueue<Runnable>(128);
    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "uCropAsyncTask #" + mCount.getAndIncrement());
        }
    };

    static {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
            sPoolWorkQueue, sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        UCROP_THREAD_POOL_EXECUTOR = threadPoolExecutor;
    }

    public static void decodeBitmapInBackground(@NonNull Context context,
        @NonNull Uri uri, @Nullable Uri outputUri,
        int requiredWidth, int requiredHeight,
        BitmapLoadCallback loadCallback) {

        new BitmapLoadTask(context, uri, outputUri, requiredWidth, requiredHeight, loadCallback)
            .executeOnExecutor(UCROP_THREAD_POOL_EXECUTOR); //Use custom thread pool
    }
.....
}

Zweihui avatar Dec 23 '20 09:12 Zweihui

I found the solution. In my case,BitmapLoadTask is not executed immediately.So I guess it may be that the thread pool in AsyncTask is full. This is the code of my solution:

public class BitmapLoadUtils {

    private static final String TAG = "BitmapLoadUtils";
    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
    public static final Executor UCROP_THREAD_POOL_EXECUTOR;
    private static final int CORE_POOL_SIZE = Math.max(2, Math.min(CPU_COUNT - 1, 4));
    private static final int MAXIMUM_POOL_SIZE = CPU_COUNT * 2 + 1;
    private static final int KEEP_ALIVE_SECONDS = 30;
    private static final BlockingQueue<Runnable> sPoolWorkQueue =
        new LinkedBlockingQueue<Runnable>(128);
    private static final ThreadFactory sThreadFactory = new ThreadFactory() {
        private final AtomicInteger mCount = new AtomicInteger(1);

        public Thread newThread(Runnable r) {
            return new Thread(r, "uCropAsyncTask #" + mCount.getAndIncrement());
        }
    };

    static {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
            sPoolWorkQueue, sThreadFactory);
        threadPoolExecutor.allowCoreThreadTimeOut(true);
        UCROP_THREAD_POOL_EXECUTOR = threadPoolExecutor;
    }

    public static void decodeBitmapInBackground(@NonNull Context context,
        @NonNull Uri uri, @Nullable Uri outputUri,
        int requiredWidth, int requiredHeight,
        BitmapLoadCallback loadCallback) {

        new BitmapLoadTask(context, uri, outputUri, requiredWidth, requiredHeight, loadCallback)
            .executeOnExecutor(UCROP_THREAD_POOL_EXECUTOR); //Use custom thread pool
    }
.....
}

Did it work?

jin123d avatar Mar 01 '21 05:03 jin123d