来自android内部存储的file.delete()返回false

时间:2014-02-13 09:46:40

标签: android file file-io android-file internal-storage

我有一种方法可以从网址下载图片并将其保存在内部存储空间的文件夹中

 public void saveDynamicImage(String url,String fileName, String folderName) {

    InputStream iStream;

    BufferedInputStream buffInputStream;
    ByteArrayBuffer byteArray = null;

    try {
        HttpGet httpGet = new HttpGet(url);
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpGet);
        iStream = httpResponse.getEntity().getContent();

        buffInputStream = new BufferedInputStream(iStream, 8 * 1024);
        byteArray = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = buffInputStream.read()) != -1) {
            byteArray.append((byte) current);
        } 

    } catch (ClientProtocolException e1) {
    } catch (IOException e1) {
    }

    File dynamicImageDir = context.getDir(AppConstants.DYNAMIC_IMAGE, Context.MODE_PRIVATE);
    File appNamefileDir = new File(dynamicImageDir, BaseActivity.appDataStore.getAppName());
    appNamefileDir.mkdirs();
    File controlNameDir = new File(appNamefileDir, folderName);
    controlNameDir.mkdirs();
    File file = new File(controlNameDir, fileName);

    try {
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(byteArray.toByteArray());
        outputStream.close();
        System.out.println("DynamicImage saving over!..");
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

我想在某个时间点删除整个目录。我删除整个目录的方法是

public void deleteDynamicImage() throws NullPointerException,FileNotFoundException {
    File rootDirectory = context.getDir(AppConstants.DYNAMIC_IMAGE, Context.MODE_WORLD_WRITEABLE);
    boolean status = rootDirectory.delete();
    Log.e("", "delete : "+status);

}

我的状态为'false'。文件已创建且工作正常。只有删除的问题。我有什么遗失的东西吗?

4 个答案:

答案 0 :(得分:20)

您的文件是目录吗?

如果是,则需要先删除此文件夹中的文件 这段代码运作良好

public void deleteDirectory(File file) {
    if( file.exists() ) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for(int i=0; i<files.length; i++) {
                if(files[i].isDirectory()) {
                    deleteDirectory(files[i]);
                }
                else {
                    files[i].delete();
                }
            }
        }
            file.delete();
    }
}

答案 1 :(得分:6)

删除目录使用此:

public void DeleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) for (File child : fileOrDirectory.listFiles())
    DeleteRecursive(child);
    fileOrDirectory.delete();
}

答案 2 :(得分:2)

您正在尝试删除目录。 File.delete()仅在目录为空时才对目录起作用

答案 3 :(得分:1)

如何在Android中删除文件:

public void deleteFile(String filePath){
    if(filePath.startsWith("content://")){
        ContentResolver contentResolver = getActivity().getContentResolver();
        contentResolver.delete(Uri.parse(filePath), null, null);
    }else {
        File file = new File(filePath);
        if(file.exists()) {
            if (file.delete()) {
                Log.e(TAG, "File deleted.");
            }else {
                Log.e(TAG, "Failed to delete file!");
            }
        }else {
            Log.e(TAG, "File not exist!"); 
        }
    }
}

重要说明: 如果您从Uri [不要使用Uri.toString()]获取文件路径,因为它将以 file:/// 格式返回文件路径,在此案例[新文件(filePath)]无效。因此,要获取文件路径,请始终使用 Uri.getPath()