how to print all file name present in assest folder .? - android

I have assest in which there are subfolder and files in this way ..
As show in image ..assest->www ->js or cs folder..then files
assest
|
|__www
|
|___js
| |
| |__a.js
|
|____cs
| |
| |__a.css
|
|____index.html
I need to print all files name ..can we print that files name
I make this function but not work ..
package com.mobilecem.atms;
import java.io.IOException;
import android.content.Context;
import android.util.Log;
public class GlobalFunction {
public static boolean listAssetFiles(String path,Context context) {
String [] list;
try {
list = context.getAssets().list(path);
if (list.length > 0) {
// This is a folder
for (String file : list) {
if (!listAssetFiles(path + "/" + file, context))
return false;
}
} else {
Log.d("File Name", "file present");
// This is a file
// TODO: add file name to an array list
}
}catch (IOException e) {
return false;
}
return true;
}
}
call like that
GlobalFunction.listAssetFiles("", aaa.this);
Result :
list have : index.html , a.js, a css
Comment

Here you go
You will have all files and its sub folders details in files List
Check it out
List<String> files = new ArrayList<String>();
public boolean listAssetFiles(String path, Context context) {
String[] list;
try {
list = context.getAssets().list(path);
if (list.length > 0) {
// This is a folder
for (String file : list) {
String root = TextUtils.isEmpty(path) ? "" : path + "/";
String fullpath = root + file;
files.add(fullpath);
if (!listAssetFiles(root + file, context))
return false;
}
} else {
// This is a file
// TODO: add file name to an array list
}
} catch (IOException e) {
return false;
}
return true;
}

This is fully woriking and showing folder names ive just tested it
private void listFiles(String dirFrom) throws IOException {
Resources res = getResources(); // if you are in an activity
AssetManager am = res.getAssets();
String[] fileList = am.list(dirFrom);
ArrayList<Drawable> dList = new ArrayList<>();
if (fileList != null) {
for (int i = 0; i < fileList.length; i++) {
Log.d("FOLDER", fileList[i]);
}
}
Call the listAssetFiles with the root folder name of your asset folder.
listAssetFiles("root_folder_name_in_assets");
If the root folder is the asset folder then call it with
listAssetFiles("");

Related

Android internal storage mechanism

I am trying to understand how Android internal storage works.
For that I read a few tutorials and a number of posts on StackOverFlow.
Nevertheless things are not all that clear when testing real code.
Here is one problem, the following code is meant to create a directory and a file:
val dirName = "myNewDir"
createDir(dirName)
val fileName = "myNewFile"
writeToFile("Some random text for testing purpose ...",fileName)
val directory = filesDir
val files: Array<File> = directory.listFiles()
println("Files count = "+files.size)
for (f in files) {
println("Name: "+f.name)
}
This is the code for the two functions createDir() and writeToFile() :
fun createDir(dirName:String): File? {
return applicationContext.getDir(dirName, MODE_PRIVATE)
} /* End of createDir */
fun writeToFile(dataBufr:String, fileName:String) {
applicationContext.openFileOutput(fileName, Context.MODE_PRIVATE).use {
output -> output.write(dataBufr.toByteArray())
}
} /* End of writeToFile */
And this is what appears in the console when executing the code above:
I/System.out: Files count = 1
I/System.out: Name: myNewFile
My question is:
Why is the directory (myNewDir) not created or at list does not appear in the console?
Latest android version, all default dirs will be created in the first open by LoadAPK.java, getDir will get the exists dir.
public File getDir(String name, int mode) {
checkMode(mode);
name = "app_" + name;
File file = makeFilename(getDataDir(), name);
if (!file.exists()) {
file.mkdir();
setFilePermissionsFromMode(file.getPath(), mode,
FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);
}
return file;
}
In the getDataDir()
#Override
public File getDataDir() {
if (mPackageInfo != null) {
File res = null;
if (isCredentialProtectedStorage()) {
res = mPackageInfo.getCredentialProtectedDataDirFile();
} else if (isDeviceProtectedStorage()) {
res = mPackageInfo.getDeviceProtectedDataDirFile();
} else {
res = mPackageInfo.getDataDirFile();
}
if (res != null) {
if (!res.exists() && android.os.Process.myUid() == android.os.Process.SYSTEM_UID) {
Log.wtf(TAG, "Data directory doesn't exist for package " + getPackageName(),
new Throwable());
}
return res;
} else {
throw new RuntimeException(
"No data directory found for package " + getPackageName());
}
} else {
throw new RuntimeException(
"No package details found for package " + getPackageName());
}
}
In the frameworks/base/core/java/android/app/LoadedApk.java all file will be init by ApplicationInfo.
private void setApplicationInfo(ApplicationInfo aInfo) {
final int myUid = Process.myUid();
aInfo = adjustNativeLibraryPaths(aInfo);
mApplicationInfo = aInfo;
mAppDir = aInfo.sourceDir;
mResDir = aInfo.uid == myUid ? aInfo.sourceDir : aInfo.publicSourceDir;
mLegacyOverlayDirs = aInfo.resourceDirs;
mOverlayPaths = aInfo.overlayPaths;
mDataDir = aInfo.dataDir;
mLibDir = aInfo.nativeLibraryDir;
mDataDirFile = FileUtils.newFileOrNull(aInfo.dataDir);
mDeviceProtectedDataDirFile = FileUtils.newFileOrNull(aInfo.deviceProtectedDataDir);
mCredentialProtectedDataDirFile = FileUtils.newFileOrNull(aInfo.credentialProtectedDataDir);
mSplitNames = aInfo.splitNames;
mSplitAppDirs = aInfo.splitSourceDirs;
mSplitResDirs = aInfo.uid == myUid ? aInfo.splitSourceDirs : aInfo.splitPublicSourceDirs;
mSplitClassLoaderNames = aInfo.splitClassLoaderNames;
if (aInfo.requestsIsolatedSplitLoading() && !ArrayUtils.isEmpty(mSplitNames)) {
mSplitLoader = new SplitDependencyLoaderImpl(aInfo.splitDependencies);
}
}
frameworks/base/core/java/android/os/FileUtils.java
public static #Nullable File newFileOrNull(#Nullable String path) {
return (path != null) ? new File(path) : null;
}
public static #Nullable File createDir(File baseDir, String name) {
final File dir = new File(baseDir, name);
return createDir(dir) ? dir : null;
}
public static boolean createDir(File dir) {
if (dir.mkdir()) {
return true;
}
if (dir.exists()) {
return dir.isDirectory();
}
return false;
}

External SDCard file path for Android

Is it true that the file path to external SDCard on Android devices are always "/storage/extSdCard"? If not, how many variations are there?
I need it for my App to test the availability of external SDCard.
I am using Titanium, it has a method Titanium.Filesystem.isExternalStoragePresent( )
but it always return true even external SDCard is not mounted.
I think it detect SDCard at local storage thus return true. But what I really want is detect whether physical SDCard is mounted or not.
Can I do this by detecting the existence of file "/storage/extSdCard" alone?
Thanks.
Is it true that the file path to external SDCard on Android devices are always "/storage/extSdCard"? If not, how many variations are there?
Sadly the path to the external storage is not always the same according to manufacturer. Using Environment.getExternalStorageDirectory() will return you the normal path for SD card which is mnt/sdcard/. But for Samsung devices for example, the SD card path is either under mnt/extSdCard/ or under mnt/external_sd/.
So one way to proceed would be to check the existence of external directory according to the path used by each manufacturer. With something like this:
mExternalDirectory = Environment.getExternalStorageDirectory()
.getAbsolutePath();
if (android.os.Build.DEVICE.contains("samsung")
|| android.os.Build.MANUFACTURER.contains("samsung")) {
File f = new File(Environment.getExternalStorageDirectory()
.getParent() + "/extSdCard" + "/myDirectory");
if (f.exists() && f.isDirectory()) {
mExternalDirectory = Environment.getExternalStorageDirectory()
.getParent() + "/extSdCard";
} else {
f = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/external_sd" + "/myDirectory");
if (f.exists() && f.isDirectory()) {
mExternalDirectory = Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/external_sd";
}
}
}
But what I really want is detect whether physical SDCard is mounted or not.
I didn't try the code yet, but the approach of Dmitriy Lozenko in this answer is much more interesting. His method returns the path of all mounted SD cards on sytem regardless of the manufacturer.
This is how I finally got sdcard path using :
public String getExternalStoragePath() {
String internalPath = Environment.getExternalStorageDirectory().getAbsolutePath();
String[] paths = internalPath.split("/");
String parentPath = "/";
for (String s : paths) {
if (s.trim().length() > 0) {
parentPath = parentPath.concat(s);
break;
}
}
File parent = new File(parentPath);
if (parent.exists()) {
File[] files = parent.listFiles();
for (File file : files) {
String filePath = file.getAbsolutePath();
Log.d(TAG, filePath);
if (filePath.equals(internalPath)) {
continue;
} else if (filePath.toLowerCase().contains("sdcard")) {
return filePath;
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
if (Environment.isExternalStorageRemovable(file)) {
return filePath;
}
} catch (RuntimeException e) {
Log.e(TAG, "RuntimeException: " + e);
}
}
}
}
return null;
}
I hope it will be useful for you :)
import android.os.Environment;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class MemoryStorage {
private MemoryStorage() {}
public static final String SD_CARD = "sdCard";
public static final String EXTERNAL_SD_CARD = "externalSdCard";
/**
* #return True if the external storage is available. False otherwise.
*/
public static boolean isAvailable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
public static String getSdCardPath() {
return Environment.getExternalStorageDirectory().getPath() + "/";
}
/**
* #return True if the external storage is writable. False otherwise.
*/
public static boolean isWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/**
* #return A map of all storage locations available
*/
public static Map<String, File> getAllStorageLocations() {
Map<String, File> map = new HashMap<String, File>(10);
List<String> mMounts = new ArrayList<String>(10);
List<String> mVold = new ArrayList<String>(10);
mMounts.add("/mnt/sdcard");
mVold.add("/mnt/sdcard");
try {
File mountFile = new File("/proc/mounts");
if (mountFile.exists()) {
Scanner scanner = new Scanner(mountFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.startsWith("/dev/block/vold/")) {
String[] lineElements = line.split(" ");
String element = lineElements[1];
// don't add the default mount path
// it's already in the list.
if (!element.equals("/mnt/sdcard"))
mMounts.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
try {
File voldFile = new File("/system/etc/vold.fstab");
if (voldFile.exists()) {
Scanner scanner = new Scanner(voldFile);
while (scanner.hasNext()) {
String line = scanner.nextLine();
if (line.startsWith("dev_mount")) {
String[] lineElements = line.split(" ");
String element = lineElements[2];
if (element.contains(":"))
element = element.substring(0, element.indexOf(":"));
if (!element.equals("/mnt/sdcard"))
mVold.add(element);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < mMounts.size(); i++) {
String mount = mMounts.get(i);
if (!mVold.contains(mount))
mMounts.remove(i--);
}
mVold.clear();
List<String> mountHash = new ArrayList<String>(10);
for (String mount : mMounts) {
File root = new File(mount);
if (root.exists() && root.isDirectory() && root.canWrite()) {
File[] list = root.listFiles();
String hash = "[";
if (list != null) {
for (File f : list) {
hash += f.getName().hashCode() + ":" + f.length() + ", ";
}
}
hash += "]";
if (!mountHash.contains(hash)) {
String key = SD_CARD + "_" + map.size();
if (map.size() == 0) {
key = SD_CARD;
} else if (map.size() == 1) {
key = EXTERNAL_SD_CARD;
}
mountHash.add(hash);
map.put(key, root);
}
}
}
mMounts.clear();
if (map.isEmpty()) {
map.put(SD_CARD, Environment.getExternalStorageDirectory());
}
return map;
}
}
I just figured out something. At least for my Android Emulator, I had the SD Card Path like ' /storage/????-???? ' where every ? is a capital letter or a digit.
So, if /storage/ directory has a directory which is readable and that is not the internal storage directory, it must be the SD Card.
My code worked on my android emulator!
String removableStoragePath;
File fileList[] = new File("/storage/").listFiles();
for (File file : fileList)
{ if(!file.getAbsolutePath().equalsIgnoreCase(Environment.getExternalStorageDirectory().getAbsolutePath()) && file.isDirectory() && file.canRead())
removableStoragePath = file.getAbsolutePath(); }
//If there is an SD Card, removableStoragePath will have it's path. If there isn't it will be an empty string.
If there is an SD Card, removableStoragePath will have it's path. If there isn't it will be an empty string.
I have got solution on this after 4 days, Please note following points while giving path to File class in Android(Java):
Use path for internal storage String
path="/storage/sdcard0/myfile.txt";
use path for external storage
path="/storage/sdcard1/myfile.txt";
mention permissions in Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
First check file length for confirmation.
Check paths in ES File Explorer regarding sdcard0 & sdcard1 is
this same or else...
e.g.:
File file = new File(path);
long = file.length();//in Bytes

List of files in assets folder and its subfolders

I have some folders with HTML files in the "assets" folder in my Android project. I need to show these HTML files from assets' sub-folders in a list. I already wrote some code about making this list.
lv1 = (ListView) findViewById(R.id.listView);
// Insert array in ListView
// In the next row I need to insert an array of strings of file names
// so please, tell me, how to get this array
lv1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filel));
lv1.setTextFilterEnabled(true);
// onclick items in ListView:
lv1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
//Clicked item position
String itemname = new Integer(position).toString();
Intent intent = new Intent();
intent.setClass(DrugList.this, Web.class);
Bundle b = new Bundle();
//I don't know what it's doing here
b.putString("defStrID", itemname);
intent.putExtras(b);
//start Intent
startActivity(intent);
}
});
private boolean listAssetFiles(String path) {
String [] list;
try {
list = getAssets().list(path);
if (list.length > 0) {
// This is a folder
for (String file : list) {
if (!listAssetFiles(path + "/" + file))
return false;
else {
// This is a file
// TODO: add file name to an array list
}
}
}
} catch (IOException e) {
return false;
}
return true;
}
Call the listAssetFiles with the root folder name of your asset folder.
listAssetFiles("root_folder_name_in_assets");
If the root folder is the asset folder then call it with
listAssetFiles("");
try this it will work in your case
f = getAssets().list("");
for(String f1 : f){
Log.v("names",f1);
}
The above snippet will show the contents of the asset root.
For example... if below is the asset structure..
assets
|__Dir1
|__Dir2
|__File1
Snippet's output will be ....
Dir1 Dir2 File1
If you need the contents of the Directory Dir1
Pass the name of Directory in the list function.
f = getAssets().list("Dir1");
Hope This Help:
following code will copy all the folder and it's content and content of sub folder to sdcard location:
private void getAssetAppFolder(String dir) throws Exception{
{
File f = new File(sdcardLocation + "/" + dir);
if (!f.exists() || !f.isDirectory())
f.mkdirs();
}
AssetManager am=getAssets();
String [] aplist=am.list(dir);
for(String strf:aplist){
try{
InputStream is=am.open(dir+"/"+strf);
copyToDisk(dir,strf,is);
}catch(Exception ex){
getAssetAppFolder(dir+"/"+strf);
}
}
}
public void copyToDisk(String dir,String name,InputStream is) throws IOException{
int size;
byte[] buffer = new byte[2048];
FileOutputStream fout = new FileOutputStream(sdcardLocation +"/"+dir+"/" +name);
BufferedOutputStream bufferOut = new BufferedOutputStream(fout, buffer.length);
while ((size = is.read(buffer, 0, buffer.length)) != -1) {
bufferOut.write(buffer, 0, size);
}
bufferOut.flush();
bufferOut.close();
is.close();
fout.close();
}
Here is a solution to my problem that I found out working 100% listing all directories and files even sub-directories and files in subdirectories.
Note: In my case
Filenames had a . in them. i.e. .htm .txt etc
Directorynames did not have any . in them.
listAssetFiles2(path); // <<-- Call function where required
//function to list files and directories
public void listAssetFiles2 (String path){
String [] list;
try {
list = getAssets().list(path);
if(list.length > 0){
for(String file : list){
System.out.println("File path = "+file);
if(file.indexOf(".") < 0) { // <<-- check if filename has a . then it is a file - hopefully directory names dont have .
System.out.println("This is a folder = "+path+"/"+file);
listAssetFiles2(file); // <<-- To get subdirectory files and directories list and check
}else{
System.out.println("This is a file = "+path+"/"+file);
}
}
}else{
System.out.println("Failed Path = "+path);
System.out.println("Check path again.");
}
}catch(IOException e){
e.printStackTrace();
}
}//now completed
Thanks
i think that this is best that check file is dir or not, altarnative try,catch!
public static List<String> listAssetFiles(Context c,String rootPath) {
List<String> files =new ArrayList<>();
try {
String [] Paths = c.getAssets().list(rootPath);
if (Paths.length > 0) {
// This is a folder
for (String file : Paths) {
String path = rootPath + "/" + file;
if (new File(path).isDirectory())
files.addAll(listAssetFiles(c,path));
else files.add(path);
}
}
} catch (IOException e) {
e.printStackTrace();
}
return files;
}
Based on the #Kammaar answer. This kotlin code scans the file tree for the leafs:
private fun listAssetFiles(path: String, context: Context): List<String> {
val result = ArrayList<String>()
context.assets.list(path).forEach { file ->
val innerFiles = listAssetFiles("$path/$file", context)
if (!innerFiles.isEmpty()) {
result.addAll(innerFiles)
} else {
// it can be an empty folder or file you don't like, you can check it here
result.add("$path/$file")
}
}
return result
}
This method return file names in a directory in Assets folder
private fun getListOfFilesFromAsset(path: String, context: Context): ArrayList<String> {
val listOfAudioFiles = ArrayList<String>()
context.assets.list(path)?.forEach { file ->
val innerFiles = getListOfFilesFromAsset("$path/$file", context)
if (innerFiles.isNotEmpty()) {
listOfAudioFiles.addAll(innerFiles)
} else {
// it can be an empty folder or file you don't like, you can check it here
listOfAudioFiles.add("$path/$file")
}
}
return listOfAudioFiles
}
For example you want to load music file path from sound folder
You can fetch all sound like this:
private const val SOUND_DIRECTORY = "sound"
fun fetchSongsFromAssets(context: Context): ArrayList<String> {
return getListOfFilesFromAsset(SOUND_DIRECTORY, context)
}
public static String[] getDirectoryFilesRecursive(String path)
{
ArrayList<String> result = new ArrayList<String>();
try
{
String[] files = Storage.AssetMgr.list(path);
for(String file : files)
{
String filename = path + (path.isEmpty() ? "" : "/") + file;
String[] tmp = Storage.AssetMgr.list(filename);
if(tmp.length!=0) {
result.addAll(Arrays.asList(getDirectoryFilesRecursive(filename)));
}
else {
result.add(filename);
}
}
}
catch (IOException e)
{
Native.err("Failed to get asset file list: " + e);
}
Object[] objectList = result.toArray();
return Arrays.copyOf(objectList,objectList.length,String[].class);
}

How to list all the files in a custom Directory

I'm trying to list all the files in a directory I have made, when I create the directory I warp a file for each contact into the dir. I then want to be able to list all those files inside/within the directory. I have tried everything including
String a = listFiles().tostring();
Yet, nothing happens. To sum it up, I want to list all the files within a custom dir in the SD card.
Here's my updated code
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
FileInputStream iStream = new FileInputStream(path);
String read = path.getbytes().tostring();
You have to see this tutorial how to build an android file browser it will help you a lot!!
This one list all folder and files in sdcard you can adapt it to what you need by changing the value of currentDir in the code
This code is travel entire sdcard and list files. that's may be helpful to you ..!
import java.io.*;
import java.util.*;
public class DirUtils {
public static List recurseDir(String dir) {
String result, _result[];
result = recurseInDirFrom(dir);
_result = result.split("\\|");
return Arrays.asList(_result);
}
private static String recurseInDirFrom(String dirItem) {
File file;
String result,list[];
result = dirItem;
file = new File(dirItem);
if (file.isDirectory()) {
list = file.list();
File[] fileslist = file.listFiles(new MyDocFileFilter());
if (fileslist != null) {
for (File file1: fileslist) {
System.out.println(file1.getAbsolutePath());
}
}
else {
System.out.println("No Subdirectory Found.");
}
for (int i = 0; i < list.length; i++)
result = result + "\n" + recurseInDirFrom(dirItem + File.separatorChar + list[i]);
}
return result;
}
static class MyDocFileFilter implements FileFilter
{
private final String[] myDocumentExtensions
= new String[] {".java", ".png", ".html", "class"};
public boolean accept(File file) {
if (!file.isFile())
return false;
for (String extension : myDocumentExtensions) {
if (file.getName().toLowerCase().endsWith(extension))
return true;
}
return false;
}
}
public static void main(String arg[]) {
DirUtils.recurseDir("your path ");
}
}

How to delete a whole folder and content?

I want the users of my application to be able to delete the DCIM folder (which is located on the SD card and contains subfolders).
Is this possible, if so how?
You can delete files and folders recursively like this:
void deleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
deleteRecursive(child);
fileOrDirectory.delete();
}
Let me tell you first thing you cannot delete the DCIM folder because it is a system folder. As you delete it manually on phone it will delete the contents of that folder, but not the DCIM folder. You can delete its contents by using the method below:
Updated as per comments
File dir = new File(Environment.getExternalStorageDirectory()+"Dir_name_here");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
We can use the command line arguments to delete a whole folder and its contents.
public static void deleteFiles(String path) {
File file = new File(path);
if (file.exists()) {
String deleteCmd = "rm -r " + path;
Runtime runtime = Runtime.getRuntime();
try {
runtime.exec(deleteCmd);
} catch (IOException e) { }
}
}
Example usage of the above code:
deleteFiles("/sdcard/uploads/");
In Kotlin you can use deleteRecursively() extension from kotlin.io package
val someDir = File("/path/to/dir")
someDir.deleteRecursively()
Short koltin version
fun File.deleteDirectory(): Boolean {
return if (exists()) {
listFiles()?.forEach {
if (it.isDirectory) {
it.deleteDirectory()
} else {
it.delete()
}
}
delete()
} else false
}
UPDATE
Kotlin stdlib function
file.deleteRecursively()
use below method to delete entire main directory which contains files and it's sub directory. After calling this method once again call delete() directory of your main directory.
// For to Delete the directory inside list of files and inner Directory
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
Your approach is decent for a folder that only contains files, but if you are looking for a scenario that also contains subfolders then recursion is needed
Also you should capture the return value of the return to make sure you are allowed to delete the file
and include
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in your manifest
void DeleteRecursive(File dir)
{
Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath());
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
File temp = new File(dir, children[i]);
if (temp.isDirectory())
{
Log.d("DeleteRecursive", "Recursive Call" + temp.getPath());
DeleteRecursive(temp);
}
else
{
Log.d("DeleteRecursive", "Delete File" + temp.getPath());
boolean b = temp.delete();
if (b == false)
{
Log.d("DeleteRecursive", "DELETE FAIL");
}
}
}
}
dir.delete();
}
There is a lot of answers, but I decided to add my own, because it's little different. It's based on OOP ;)
I created class DirectoryCleaner, which help me each time when I need to clean some directory.
public class DirectoryCleaner {
private final File mFile;
public DirectoryCleaner(File file) {
mFile = file;
}
public void clean() {
if (null == mFile || !mFile.exists() || !mFile.isDirectory()) return;
for (File file : mFile.listFiles()) {
delete(file);
}
}
private void delete(File file) {
if (file.isDirectory()) {
for (File child : file.listFiles()) {
delete(child);
}
}
file.delete();
}
}
It can be used to solve this problem in next way:
File dir = new File(Environment.getExternalStorageDirectory(), "your_directory_name");
new DirectoryCleaner(dir).clean();
dir.delete();
You can not delete the directory if it has subdirectories or files in Java. Try this two-line simple solution. This will delete the directory and contests inside the directory.
File dirName = new File("directory path");
FileUtils.deleteDirectory(dirName);
Add this line in gradle file and sync the project
compile 'org.apache.commons:commons-io:1.3.2'
According to the documentation:
If this abstract pathname does not denote a directory, then this method returns null.
So you should check if listFiles is null and only continue if it's not
boolean deleteDirectory(File path) {
if(path.exists()) {
File[] files = path.listFiles();
if (files == null) {
return false;
}
for (File file : files) {
if (file.isDirectory()) {
deleteDirectory(file);
} else {
boolean wasSuccessful = file.delete();
if (wasSuccessful) {
Log.i("Deleted ", "successfully");
}
}
}
}
return(path.delete());
}
If you dont need to delete things recursively you can try something like this:
File file = new File(context.getExternalFilesDir(null), "");
if (file != null && file.isDirectory()) {
File[] files = file.listFiles();
if(files != null) {
for(File f : files) {
f.delete();
}
}
}
public static void deleteDirectory( File dir )
{
if ( dir.isDirectory() )
{
String [] children = dir.list();
for ( int i = 0 ; i < children.length ; i ++ )
{
File child = new File( dir , children[i] );
if(child.isDirectory()){
deleteDirectory( child );
child.delete();
}else{
child.delete();
}
}
dir.delete();
}
}
see android.os.FileUtils, it's hide on API 21
public static boolean deleteContents(File dir) {
File[] files = dir.listFiles();
boolean success = true;
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
success &= deleteContents(file);
}
if (!file.delete()) {
Log.w("Failed to delete " + file);
success = false;
}
}
}
return success;
}
Source: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/os/FileUtils.java#414
The fastest and easiest way:
public static boolean deleteFolder(File removableFolder) {
File[] files = removableFolder.listFiles();
if (files != null && files.length > 0) {
for (File file : files) {
boolean success;
if (file.isDirectory())
success = deleteFolder(file);
else success = file.delete();
if (!success) return false;
}
}
return removableFolder.delete();
}
This is what I do... (terse and tested)
...
deleteDir(new File(dir_to_be_deleted));
...
// delete directory and contents
void deleteDir(File file) {
if (file.isDirectory())
for (String child : file.list())
deleteDir(new File(file, child));
file.delete(); // delete child file or empty directory
}
private static void deleteRecursive(File dir)
{
//Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath());
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
File temp = new File(dir, children[i]);
deleteRecursive(temp);
}
}
if (dir.delete() == false)
{
Log.d("DeleteRecursive", "DELETE FAIL");
}
}
Simple way to delete all file from directory :
It is generic function for delete all images from directory by calling only
deleteAllImageFile(context);
public static void deleteAllFile(Context context) {
File directory = context.getExternalFilesDir(null);
if (directory.isDirectory()) {
for (String fileName: file.list()) {
new File(file,fileName).delete();
}
}
}
Safest code I know:
private boolean recursiveRemove(File file) {
if(file == null || !file.exists()) {
return false;
}
if(file.isDirectory()) {
File[] list = file.listFiles();
if(list != null) {
for(File item : list) {
recursiveRemove(item);
}
}
}
if(file.exists()) {
file.delete();
}
return !file.exists();
}
Checks the file exists, handles nulls, checks the directory was actually deleted
//To delete all the files of a specific folder & subfolder
public static void deleteFiles(File directory, Context c) {
try {
for (File file : directory.listFiles()) {
if (file.isFile()) {
final ContentResolver contentResolver = c.getContentResolver();
String canonicalPath;
try {
canonicalPath = file.getCanonicalPath();
} catch (IOException e) {
canonicalPath = file.getAbsolutePath();
}
final Uri uri = MediaStore.Files.getContentUri("external");
final int result = contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{canonicalPath});
if (result == 0) {
final String absolutePath = file.getAbsolutePath();
if (!absolutePath.equals(canonicalPath)) {
contentResolver.delete(uri,
MediaStore.Files.FileColumns.DATA + "=?", new String[]{absolutePath});
}
}
if (file.exists()) {
file.delete();
if (file.exists()) {
try {
file.getCanonicalFile().delete();
} catch (IOException e) {
e.printStackTrace();
}
if (file.exists()) {
c.deleteFile(file.getName());
}
}
}
} else
deleteFiles(file, c);
}
} catch (Exception e) {
}
}
here is your solution it will also refresh the gallery as well.
This (Tries to delete all sub-files and sub-directories including the supplied directory):
If File, delete
If Empty Directory, delete
if Not Empty Directory, call delete again with sub-directory, repeat 1 to 3
example:
File externalDir = Environment.getExternalStorageDirectory()
Utils.deleteAll(externalDir); //BE CAREFUL.. Will try and delete ALL external storage files and directories
To gain access to External Storage Directory, you need the following permissions:
(Use ContextCompat.checkSelfPermission and ActivityCompat.requestPermissions)
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Recursive method:
public static boolean deleteAll(File file) {
if (file == null || !file.exists()) return false;
boolean success = true;
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f.isDirectory()) {
success &= deleteAll(f);
}
if (!f.delete()) {
Log.w("deleteAll", "Failed to delete " + f);
success = false;
}
}
} else {
if (!file.delete()) {
Log.w("deleteAll", "Failed to delete " + file);
success = false;
}
}
} else {
if (!file.delete()) {
Log.w("deleteAll", "Failed to delete " + file);
success = false;
}
}
return success;
}
Here is a non-recursive implementation, just for fun:
/**
* Deletes the given folder and all its files / subfolders.
* Is not implemented in a recursive way. The "Recursively" in the name stems from the filesystem command
* #param root The folder to delete recursively
*/
public static void deleteRecursively(final File root) {
LinkedList<File> deletionQueue = new LinkedList<>();
deletionQueue.add(root);
while(!deletionQueue.isEmpty()) {
final File toDelete = deletionQueue.removeFirst();
final File[] children = toDelete.listFiles();
if(children == null || children.length == 0) {
// This is either a file or an empty directory -> deletion possible
toDelete.delete();
} else {
// Add the children before the folder because they have to be deleted first
deletionQueue.addAll(Arrays.asList(children));
// Add the folder again because we can't delete it yet.
deletionQueue.addLast(toDelete);
}
}
}
I've put this one though its' paces it deletes a folder with any directory structure.
public int removeDirectory(final File folder) {
if(folder.isDirectory() == true) {
File[] folderContents = folder.listFiles();
int deletedFiles = 0;
if(folderContents.length == 0) {
if(folder.delete()) {
deletedFiles++;
return deletedFiles;
}
}
else if(folderContents.length > 0) {
do {
File lastFolder = folder;
File[] lastFolderContents = lastFolder.listFiles();
//This while loop finds the deepest path that does not contain any other folders
do {
for(File file : lastFolderContents) {
if(file.isDirectory()) {
lastFolder = file;
lastFolderContents = file.listFiles();
break;
}
else {
if(file.delete()) {
deletedFiles++;
}
else {
break;
}
}//End if(file.isDirectory())
}//End for(File file : folderContents)
} while(lastFolder.delete() == false);
deletedFiles++;
if(folder.exists() == false) {return deletedFiles;}
} while(folder.exists());
}
}
else {
return -1;
}
return 0;
}
Hope this helps.
Yet another (modern) way to solve it.
public class FileUtils {
public static void delete(File fileOrDirectory) {
if(fileOrDirectory != null && fileOrDirectory.exists()) {
if(fileOrDirectory.isDirectory() && fileOrDirectory.listFiles() != null) {
Arrays.stream(fileOrDirectory.listFiles())
.forEach(FileUtils::delete);
}
fileOrDirectory.delete();
}
}
}
On Android since API 26
public class FileUtils {
public static void delete(File fileOrDirectory) {
if(fileOrDirectory != null) {
delete(fileOrDirectory.toPath());
}
}
public static void delete(Path path) {
try {
if(Files.exists(path)) {
Files.walk(path)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
// .peek(System.out::println)
.forEach(File::delete);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I'm using this recursive function to do the job:
public static void deleteDirAndContents(#NonNull File mFile){
if (mFile.isDirectory() && mFile.listFiles() != null && mFile.listFiles().length > 0x0) {
for (File file : mFile.listFiles()) {
deleteDirAndContents(file);
}
} else {
mFile.delete();
}
}
The function checks if it is a directory or a file.
If it is a directory checks if it has child files, if it has child files will call herself again passing the children and repeating.
If it is a file it delete it.
(Don't use this function to clear the app cache by passing the cache dir because it will delete the cache dir too so the app will crash...
If you want to clear the cache you use this function that won't delete the dir you pass to it:
public static void deleteDirContents(#NonNull File mFile){
if (mFile.isDirectory() && mFile.listFiles() != null && mFile.listFiles().length > 0x0) {
for (File file : mFile.listFiles()) {
deleteDirAndContents(file);
}
}
}
or you can check if it is the cache dir using:
if (!mFile.getAbsolutePath().equals(context.getCacheDir().getAbsolutePath())) {
mFile.delete();
}
Example code to clear app cache:
public static void clearAppCache(Context context){
try {
File cache = context.getCacheDir();
FilesUtils.deleteDirContents(cache);
} catch (Exception e){
MyLogger.onException(TAG, e);
}
}
Bye, Have a nice day & coding :D
This is kotlin option. It worked very well.
fun executeDelete(context: Context, paths: List<String>): Int {
return try {
val files = paths.map { File(it) }
val fileCommands = files.joinToString(separator = " ") {
if (it.isDirectory) "'${it.absolutePath}/'" else "'${it.absolutePath}'"
}
val command = "rm -rf $fileCommands"
val process = Runtime.getRuntime().exec(arrayOf("sh", "-c", command))
val result = process.waitFor()
if (result == 0) {
context.rescanPaths(paths)
}
result
} catch (e: Exception) {
-1
}
}
// avoid calling this multiple times in row, it can delete whole folder contents
fun Context.rescanPaths(paths: List<String>, callback: (() -> Unit)? = null) {
if (paths.isEmpty()) {
callback?.invoke()
return
}
var cnt = paths.size
MediaScannerConnection.scanFile(applicationContext, paths.toTypedArray(), null) { _, _ ->
if (--cnt == 0) {
callback?.invoke()
}
}
}

Categories

Resources