简体   繁体   English

如何检查上下文是否仍然可以在android中使用?

[英]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. 在我的android应用中,我有一个异步任务,该任务下载一张图片并将其保存到外部存储中,然后重复执行直到下载完所有图片为止。 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? 为什么不改用ApplicationContext? At least you will be sure that the context will exist until you close the application. 至少在关闭应用程序之前,您将确保上下文将存在。

context.getApplicationContext()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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