简体   繁体   中英

How to check if context can still be used in android?

In my android app I have a async task, that downloads an image and saves it to external storage, then repeats until all images are downloaded. This can take a while, and I reference the context from the activity that called it, a couple times in the async per downloaded image.

During this process the user could have pressed back or something which ends the context.

How can I check if I can still use it in the async task?

This is my async task code:

package async;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import common.Common;
import common.FishCategory;
import http.Network;
import interfaces.ImageDownloader;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;

public class Async_getAllFishPics extends AsyncTask<FishCategory, Void, FishCategory> {

    String link;
    ImageDownloader callerContext;
    List<FishCategory> categoryList;

    public Async_getAllFishPics(ImageDownloader callerContext, List<FishCategory> categoryList) {
        this.callerContext = callerContext;
        this.categoryList = categoryList;

        for (FishCategory fish : categoryList) {
            link = fish.getLink();
            if (!link.equals("")) {
                String imgpath = Common.getImagePath(link);
                File file = new File(android.os.Environment.getExternalStorageDirectory(), imgpath);
                if (!file.exists() && Network.isNetworkAvailable((Context)callerContext)) {
                    execute(fish);
                    break;
                }
            }
        }
    }

    @Override
    protected FishCategory doInBackground(FishCategory... fishes) {
        Bitmap bitmap = Network.DownloadImage((Context) callerContext, link);

        try {
            saveImage(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return fishes[0];
    }

    @Override
    protected void onPostExecute(FishCategory fish) {
        try {
            fish.setLink(link);
            new Async_getAllFishPics(callerContext, categoryList);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    private void saveImage(Bitmap bitmap) throws IOException {
        if (bitmap != null) {
            String path = Environment.getExternalStorageDirectory().toString();

            String imgpath = Common.getImagePath(link);

            File file = new File(path, imgpath);
            file.getParentFile().mkdirs();

            String abs = file.getAbsolutePath();

            OutputStream fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

            fOut.flush();
            fOut.close();

            MediaStore.Images.Media.insertImage(((Activity)callerContext).getContentResolver(),abs,file.getName(),file.getName());
        }   
    }
}

Why don't you use the ApplicationContext instead? At least you will be sure that the context will exist until you close the application.

context.getApplicationContext()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM