0

I'm using Glide to download image as bitmap as saving it in the internal storage. I am using shared preferences to store the image path in the list. But I'm getting this error :

Caused by java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
   at com.bumptech.glide.manager.RequestManagerRetriever.assertNotDestroyed(RequestManagerRetriever.java:12)
   at com.bumptech.glide.manager.RequestManagerRetriever.get(RequestManagerRetriever.java:18)
   at com.bumptech.glide.Glide.with(Glide.java:4)
   at com.devabhishek.reels.downloader.utils.DownloadBroadcastReciever.onReceive(DownloadBroadcastReciever.java:69)
   at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1619)
   at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(-.java:2)
   at android.os.Handler.handleCallback(Handler.java:938)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:239)
   at android.app.ActivityThread.main(ActivityThread.java:8155)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:626)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1015)

My code is :

 Glide.with(context.getApplicationContext())
                        .asBitmap()
                        .load(downloadDetails.getImagePath())
                        .into(new CustomTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                                String imgStoragePath = saveImage(resource, context);
                                downloadDetails.setImagePath(imgStoragePath);
                                List<DownloadFileDetails> data = new ArrayList<>();
                                data.add(downloadDetails);
                                prefManager.saveDataList(data);
                                if (prefManager.isEmptyDownload()){
                                    prefManager.setEmptyDownload(false);
                                }
                                DownloadActivity instance  = DownloadActivity.returnInstance();
                                if (instance != null){
                                    instance.updateUI();
                                }
                            }
                            @Override
                            public void onLoadCleared(@Nullable Drawable placeholder) {

                            }
                        });


 private String saveImage(Bitmap image, Context context) {
    String savedImagePath;

    String imageFileName = System.currentTimeMillis() + ".jpg";
    File imgFile = context.getExternalFilesDir("Images");

    File imageFile = new File(imgFile, imageFileName);
    savedImagePath = imageFile.getAbsolutePath();
    try {
        OutputStream fOut = new FileOutputStream(imageFile);
        image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return savedImagePath;
}
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
Abhi
  • 13
  • 2
  • 5

1 Answers1

0

You can just try with this

Glide.with(context.getApplicationContext())

instead of

Glide.with(ClassName.this)

in your case, it will work.

If you want to use context.getApplicationContext() click here

Suhaib Janjua
  • 3,538
  • 16
  • 59
  • 73