6

我有一种方法可以从 url 下载图像并将其保存在内部存储的文件夹中

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

}

我的状态为“假”。文件已创建并且工作正常。唯一的问题是删除。有什么我想念的吗?

4

4 回答 4

25

你的文件是目录吗?

如果是,您需要先删除此文件夹中的文件,
此代码运行良好

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();
    }
}
于 2014-02-13T09:51:35.907 回答
6

要删除目录,请使用:

public void DeleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) for (File child : fileOrDirectory.listFiles())
    DeleteRecursive(child);
    fileOrDirectory.delete();
}
于 2014-02-13T09:51:42.063 回答
3

您正在尝试删除目录。File.delete()仅当目录为空时才适用于目录

于 2014-02-13T09:49:20.317 回答
3

如何在 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:///格式返回文件路径,在这种情况下 [ new File(filePath) ] 将不起作用。因此,要获取文件路径,请始终使用Uri.getPath()

于 2016-09-16T22:36:50.563 回答