Glide Image extraction

Viewed 1248

I am using glide to extract a bitmap from the cache and save it elsewhere. The relevant function below (based on this post:) fails to trigger the extraction. In fact, the log line 'Log.d(TAG, "About to start extraction");' below never gets triggered.

Any ideas on why the simple target function never gets called?

public byte[] extractImageFromCache(Context context, String pictureURL) {

    byte[] byteArray = new byte[0];
    if (context != null && pictureURL != null && !pictureURL.isEmpty()) {

        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        Glide.with(context)
            .load(pictureURL)
            .asBitmap()
            .toBytes()
            .diskCacheStrategy(DiskCacheStrategy.SOURCE) // Load the original hires image
            .into(new SimpleTarget<byte[]>() {
                @Override public void onResourceReady(final byte[] resource, GlideAnimation<? super byte[]> glideAnimation) {
                    Log.d(TAG, "About to start extraction");
                    new AsyncTask<Void, Void, Void>() {
                        @Override protected Void doInBackground(Void... params) {
                            Log.d(TAG, "Start extraction");
                            try {
                                Log.d(TAG, "Image bytes len: " + resource.length);
                                byteArrayOutputStream.write(resource);
                                byteArrayOutputStream.flush();
                            } catch (IOException e) {
                                e.printStackTrace();
                                Log.i(TAG, "Unable to load image: " + pictureURL);
                                e.printStackTrace();
                            }
                            return null;
                        }
                    }.execute();

                }
            });
        Log.d(TAG, String.format("Got image bytes: %d for %s", byteArrayOutputStream.size(), pictureURL));
        return byteArrayOutputStream.toByteArray();
    }
    return null;
}
4 Answers
Related