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

标签 java android multithreading android-asynctask thread-safety

在我的 Android 应用程序中,我有一个异步任务,它下载图像并将其保存到外部存储,然后重复,直到下载所有图像。这可能需要一段时间,并且我在每个下载图像的异步中引用了调用它的 Activity 中的上下文几次。

在此过程中,用户可能按下了回退键或执行了某些操作来结束上下文。

如何检查我是否仍然可以在异步任务中使用它?

这是我的异步任务代码:

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());
        }   
    }
}

最佳答案

为什么不使用 ApplicationContext 呢?至少您可以确定上下文将存在,直到您关闭应用程序。

context.getApplicationContext()

关于java - 如何检查上下文是否仍然可以在android中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297196/

相关文章:

java - 在构造函数中初始化值之前检查条件

java - 从非主线程读取/proc/self/exe 时,Android 权限被拒绝

android - 在不提供来源的情况下授予对 Android 服务的访问权限

带有 Facebook 登录示例的 java.lang.RuntimeException

java - 我根本没有得到线程同步

java - JDBC未连接到Web服务中的数据库

java - 如何将迷你信息添加到 Eclipse 项目文件夹?

c++ - OMP部分中的线程数

java - GUI 中的线程 (Swing) - 应用程序解冻

java - 为什么我的简化 DES 实现在 Cp1252 编码下工作正常,但在 UTF-8 编码下工作不佳?