How do I delete files programmatically on Android?
Why don't you test this with this code:
File fdelete = new File(uri.getPath());
if (fdelete.exists()) {
if (fdelete.delete()) {
System.out.println("file Deleted :" + uri.getPath());
} else {
System.out.println("file not Deleted :" + uri.getPath());
}
}
I think part of the problem is you never try to delete the file, you just keep creating a variable that has a method call.
So in your case you could try:
File file = new File(uri.getPath());
file.delete();
if(file.exists()){
file.getCanonicalFile().delete();
if(file.exists()){
getApplicationContext().deleteFile(file.getName());
}
}
However I think that's a little overkill.
You added a comment that you are using an external directory rather than a uri. So instead you should add something like:
String root = Environment.getExternalStorageDirectory().toString();
File file = new File(root + "/images/media/2918");
Then try to delete the file.
Delete a zip file after unzip in java
If file.delete()
returns false, then my guess is that another process has the zip file open - or possibly even your own process.
- Check that you've got the path correct, e.g. what does
file.exists()
return? - Check that you've got permission to delete the file as the user running your application
- Check that you haven't got an open handle to the file within your code (e.g. have you just read from it and not closed the input stream?)
- Check that you don't have the file opened in a desktop app
android unzip folder from zip file and read content from that folder
private void unzip(String src, String dest){
final int BUFFER_SIZE = 4096;
BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(src);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null){
String zipEntryName = zipEntry.getName();
String name = dest.substring(dest.lastIndexOf("/")-1);
File FileName = new File(FolderName);
if (!FileName.isDirectory()) {
try {
if (FileName.mkdir()) {
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
File file = new File(FolderName+"/" +zipEntryName);
if (file.exists()){
} else {
if(zipEntry.isDirectory()){
file.mkdirs();
}else{
byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
int count;
while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}
zipInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Try This code it's working for me.
Related Topics
Java Spring Boot Test: How to Exclude Java Configuration Class from Test Context
How to Trim White Space from All Elements in Array
How to Get the Ip of the Wifi Hotspot in Android
How to Prevent Duplicate Results in Hibernate
How to Find an Object in an Arraylist by Property
Java 8 Lambda Create List of Strings from List of Objects
Generate Bank Account Number With Random
How to Disable Error Highlighting in VS Code
Spring Boot - Exception Encountered During Context Initialization. How to Know the Reason of Error
Is There an Invisible Character That Is Not Regarded as Whitespace
Java How to Retrieve More Than 1 Million Rows in a Resultset
How to Check If an Excel Cell Is Empty Using Apache Poi
Illegal Character When Trying to Compile Java Code
Consider Defining a Bean of Type in Your Configuration
Jsonobject.Tostring: How Not to Escape Slashes