Android图片文件未创建

时间:2013-01-11 21:32:26

标签: android image file android-asynctask save

我创建了这个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不起作用?

0 个答案:

没有答案