Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file.delete() from android internal storage return false

I have a method to download a image from url and save it in a folder at 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) {
    }
}

i want to delete the whole directory at a point of time. My method to delete entire directory is

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

}

i am getting the status as 'false'. files are created and working fine. only problem in deletion. Is there any thing I am missing?

like image 476
Arundas K V Avatar asked Feb 13 '14 09:02

Arundas K V


People also ask

Can I delete the Android file from internal storage?

The most straightforward way to delete a file is to have the opened file reference call delete() on itself. myFile. delete() ; If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():

What happens if you delete system files in Android?

You can free up space on your device by clearing these files. App settings are not affected. Important: If you clear junk files or delete files using Files by Google, the data will be permanently deleted.

Which method is used to delete file in Android?

You can delete a file by using delete() method. For that first you need to create a File reference in your code by specifying the path of the file as argument, then call delete() method to delete the file.


2 Answers

Is your file a directory?

If it's, you need to delete file in this folder first
this code is work well

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();
    }
}
like image 109
henry4343 Avatar answered Sep 24 '22 02:09

henry4343


To delete Directory use this:

public void DeleteRecursive(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) for (File child : fileOrDirectory.listFiles())
    DeleteRecursive(child);
    fileOrDirectory.delete();
}
like image 34
Vikas Rathod Avatar answered Sep 26 '22 02:09

Vikas Rathod