0

我创建了这个 AsyncTask 来下载图像并将其保存到手机中。如果图像已经存在,则应该跳过下载图像的代码,但每次到达 f.exists() 时,即使图像之前已经保存,它也是错误的。为什么会这样?

private class fanartDownloader extends AsyncTask<String, Integer, String> {
        //First argument is image url and the second is the show id
        @Override
        protected String doInBackground(String... args) {
            String fanartUrl = args[0];
            fanartUrl = fanartUrl.substring(0, fanartUrl.length() - 4);
            //Add proper end for small image
            fanartUrl += SMALL_FANART_URL_END;
            try {
                String path = getApplicationContext().getFilesDir().toString();
                path = path + "/" + args[1] + "/";
                File f = new File(path, "fanart.jpg");
                if (f.exists()) {

                }
                else {
                    f.mkdir();
                    URL url_value = new URL(fanartUrl);
                    Bitmap fanart = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
                    FileOutputStream out = new FileOutputStream(path);
                    fanart.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    out.close();
                }

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    }
}

现在我用这个略有不同的迭代解决了这个问题:

private class fanartDownloader extends AsyncTask<String, Integer, String> {
        //First argument is image url and the second is the show id
        @Override
        protected String doInBackground(String... args) {
            String fanartUrl = args[0];
            fanartUrl = fanartUrl.substring(0, fanartUrl.length() - 4);
            //Add proper end for small image
            fanartUrl += SMALL_FANART_URL_END;
            try {
                String file = args[1] + "_" + "fanart.jpg";
                String path = getApplicationContext().getFilesDir().toString();
                path = path + "/" + file;
                File f = new File(path);
                if (f.exists()) {

                }
                else {
                    URL url_value = new URL(fanartUrl);
                    Bitmap fanart = BitmapFactory.decodeStream(url_value.openConnection().getInputStream());
                    FileOutputStream out = getApplicationContext().openFileOutput(file, MODE_PRIVATE);
                    fanart.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    out.close();
                }

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    }
}

有谁知道为什么第一个 AsyncTask 不起作用?

4

0 回答 0