I cant delete the folder in storage. This is my code
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/sdcard/folder" );
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
This is how you can do it
but first you need to have this permission in menifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and then
File dir = new File(Environment.getExternalStorageDirectory()+"/sdcard/folder");
if (dir.isDirectory())
{
String[] children = dir.list();
for (int i = 0; i < children.length; i++)
{
new File(dir, children[i]).delete();
}
}
Related
I'm making application that find specific file in my storage.
So I put all file list into List.
Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));
void searchFile(File directory){
File[] files = directory.listFiles();
try{
if(directory.exists()) {
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++) {
if(files[i].exists()) {
if (files[i].isDirectory()) {
File[] file = files[i].listFiles();
for (int j = 0; j < file.length; j++) {
searchFile(file[j].getPath());
}
} else
fileList.add(files[i].getPath());
}
}
}
} catch(Exception ex){}
}
But My the number of all file is more than 60000.
So When I tried to debug, It worked so slowly.
How can I get All file list in my storage quickly?
Please try the below code.
Root = Environment.getExternalStorageDirectory().getAbsolutePath();
List<String> fileList = new ArrayList<String>();
searchFile(new File(Root));
void searchFile(File directory){
try{
if(directory.exists()) {
File[] files = directory.listFiles();
for (File file : files) {
if(file.exists()) {
if (file.isDirectory()) {
File[] innerFiles = file.listFiles();
for (File innerFile : innerFiles) {
searchFile(innerile.getPath());
}
} else
fileList.add(file.getPath());
}
}
}
} catch(Exception ex){}
}
This will narrow down your performance issue to a certain level.
I have a problem getting the size of a file.
I have the following code:
File file = new File(path);
FilenameFilter mediafilefilter = new FilenameFilter(){
private String[] filter = {".txt"};
#Override
public boolean accept(File dir, String filename) {
for(int i= 0;i< filter.length ; i++){
if(filename.indexOf(filter[i]) != -1)return true;
}
return false;
}
};
File[] flies = file.listFiles(mediafilefilter);
if (files != null) {
{
if (files.length > 0)
{
System.out.println("Totol is :" + files.length);
for (int j = 0; j < files.length; j++) //not work
}
}
}
some text file is 0 byte
Like that
list[0].length()/1024
list[0] is the first file in your array
public long getFileSizes(File f) throws Exception{
long s=0;
if (f.exists()) {
FileInputStream fis = null;
fis = new FileInputStream(f);
s= fis.available();
}
return s;
}
I know how to list the files, but
getFilesDir()
and
Environment.getExternalStorageDirectory()
both gives internal storage list
Please help me
Full Code
Internal
File dir = new File(getFilesDir().getAbsolutePath());
File[] list = dir.listFiles();
String[] name = new String[list.length];
for (int i = 0; i < name.length; i++) {
name[i] = list[i].getName();
}
ListAdapter adapter = new FilesAdapter(this, android.R.layout.simple_list_item1, name);
ListView listView = (ListView) findViewById(R.id.internal_list);
listView.setAdapter(adapter);
External
File sdcard = Environment.getExternalStorageDirectory();
File dir = new File(sdcard.getAbsolutePath());
File[] list = dir.listFiles();
String[] name = new String[list.length];
for (int i = 0; i < name.length; i++) {
name[i] = list[i].getName();
}
ListAdapter adapter = new FilesAdapter(this, android.R.layout.simple_list_item1, name);
ListView listView = (ListView) findViewById(R.id.internal_list);
listView.setAdapter(adapter);
Thanks
This is any way to delete only .jpg files from folder?
This is my remove method:
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
How can I remove only .jpg files from folder?
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
String filename = children[i];
if (filename.endsWith(".jpeg") || filename.endsWith(".jpg"))
new File(dir, filename).delete();
}
}
or you prefer the for-each version
if (dir.isDirectory()) {
String[] children = dir.list();
for (String child : children) {
if (child.endsWith(".jpeg") || child.endsWith(".jpeg"))
new File(dir, child).delete();
}
}
Try like this
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
if(children[i].endsWith('.jpg' || children[i].endsWith('.jpeg'))
{
new File(dir, children[i]).delete();
}
}
}
File dir = new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
Then call
walkdir(dir);
public void walkdir(File dir) {
String Patternjpg = ".jpg";
File listFile[] = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
walkdir(listFile[i]);
} else {
if (listFile[i].getName().endsWith(Patternjpg)){
//Do what ever u want
listFile[i].delete();
}
}
}
}
}
How does one obtain images from the /DCIM/100ANDRO folder?
I have tried
File rootsd = Environment.getExternalStorageDirectory();
File dcim = new File(rootsd.getAbsolutePath() + "/DCIM/100ANDRO");
File[] imagelist = dcim.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
but I got a Null Pointer Exception.
Have you remembered to give the app permission to write the external memory?
It is done in the AndroidManifest.xml file, by adding
"android.permission.WRITE_EXTERNAL_STORAGE"
Since you are making a new file object, this is needed, I would guess.