保存图像文件时出错:没有此类文件或目录

时间:2015-05-25 08:04:27

标签: android image download server

我在下载图片时遇到以下错误:

Error saving image file: /storage/emulated/0/InstagramFavouriteshttps:/scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e15/11371164_841887855880940_277042645_n.jpg: open failed: ENOENT (No such file or directory)

以下是我的代码:

private boolean storeImage(Bitmap imageData, String filename) {
        //get path to external storage (SD card)
       //String iconsStoragePath = Environment.getExternalStorageDirectory() + "/myAppDir/myImages/"

        File newfile = new File(rootPath);
        //create storage directories, if they don't exist
        //rootFileDirectory.mkdirs();
        System.out.println("IMAGES DOWNLOADED IN FOLDER=="+newfile);
        try {
            String filePath = newfile.toString() + filename;
            FileOutputStream fileOutputStream = new FileOutputStream(filePath);

            BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

            //choose another format if PNG doesn't suit you
            imageData.compress(CompressFormat.PNG, 100, bos);

            bos.flush();
            bos.close();

        } catch (FileNotFoundException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
            return false;
        } catch (IOException e) {
            Log.w("TAG", "Error saving image file: " + e.getMessage());
            return false;
        }

        return true;
    }

1 个答案:

答案 0 :(得分:0)

在清单中添加此权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

编辑:

您需要在行

下方为最新路径指定新文件
filePath = newfile.toString() + filename;

你应该添加:

newfile = new File(filePath);