File not deleting successfully in android - android

I have several image files in my sdcard inside the folder req_images. I want to delete all the image files in this folder. So I wrote the following code. The arraylist f stores the list of all image files in it in the form /mnt/sdcard/req_images/Image-1.jpg. When I try to delete all the files one by one using the for loop, the files are not deleting from my sd card. The boolean result which I get as output was false while I try to delete all the files. The file is still there in the sd card and I have checked it. What is the possible error in it.
ArrayList<String> f = new ArrayList<String>(); // list of available files in path
File[] listFile;
public void getSdcardImages()
{
File file= new File(android.os.Environment.getExternalStorageDirectory(),"req_images");
if (file.isDirectory())
{
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++)
{
f.add(listFile[i].getAbsolutePath());
}
}
}
for(int h=0;h<f.size();h++)
{
File fil=new File(f.get(h));
Boolean bool= fil.delete();
Log.d("File deletion status"+h,bool.toString());
}

Instead of doing it this way try a different approach
File dir = new File(Environment.getExternalStorageDirectory() + "/req_images");
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}

Related

Activity to search all music files (or a particular extension) in Android storage

I am building an Android app that writes files in the format of my custom .fire extension.
I need to access them all at once and put them in a ListView. Could someone please help me to fetch them via an activity or Async Task Manager?
You can write an Async Task to fetch the .fire files from the folder into which you are writing them initially and dynamically add the filenames into the listview.
You can do something like this in your async task:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/"+FolderWhichContainsyourFiles);
File[] listOfFiles = dir.listFiles();
for (int count = 0; count < listOfFiles.length; count++) {
File file = listOfFiles[count];
if (file.isFile() && file.getName().endsWith(".fire")) {
String fileName = file.getName();
//Here you can dynamically add this name into the ListView
}
}
}
You need to use an ArrayAdapter and add elements to the ArrayAdapter dynamically. It is brilliantly explained here: Dynamically add elements to a listView Android
if you want to find files recursively:
private static List<File> filesList = new ArrayList<File>();
public static List<File> recursiveList(String filePath) {
try {
File[] dir = new File(filePath).listFiles();
for (File item : dir) {
if (item.isDirectory()) recursiveList(item.getAbsolutePath());
else if (item.isFile() && item.getName().endsWith(".fire"))
filesList.add(item);
}
return filesList;
} catch (Exception e) {
return null;
}
}
and to retrieve files in activity:
List<File> files = recursiveList("/sdcard/Music/");
for (File file : files)
adapterFilesList.add("" + file.getName());

Delete files from a known folder

I'm trying to delete all files that are exists in a known path.
I've used the next function to do so -
public void deleteAllImages(){
Log.d(TAG, "ENTERD DELETED ALL IMAGES ");
String path = "/data/data/yourapp/app_data/imageDir/";
File directory = new File(path);
if (directory.isDirectory()){
Log.d(TAG, "ENTERED IF ");
for (File child : directory.listFiles()){
Log.d(TAG, "ENTERED FOR "+ child);
child.delete();
}
}
}
But it seem that it never getting into the if statement - guess it mean that it doesn't treat directory as one. So what am I doing wrong here?
Most likely the path is not correct. The isDirectory() will return false in the following cases:
The path points to file (obviously), and not to directory.
The path
is invalid (i.e. there is no such file/directory exists).
There is
not enough permissions granted to your application to determine
whether path points to directory.
Try this :
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
new File(dir, children[i]).delete();
}
}
Check this : Delete files from folder
Hope this helps.
try this...
String path = "/data/data/yourapp/app_data/imageDir/";
File file = new File(path);
if(file.isDirectory()) {
File[] files = file.listFiles();
if(files != null && files.length > 0) {
for (File file2 : files) {
file2.delete();
}
}
}

Android How can I read filenames from a specific folder?

I would like to scan a specific folders for all filenames inside to save them to an array.
But how can I access the sd Card to do this?
Help would be great (io / enviroment???)
Environment.getExternalStorageDirectory() will link you to the SD card if one is inserted in the phone. Otherwise it just links you to your phone storage. Here's a link to more information on Environment http://developer.android.com/reference/android/os/Environment.html.
File file = new File(Environment.getExternalStorageDirectory()+"/folder/");
File[] files = file.listFiles();
String[] fileNames = new String[files.length];
for(int i = 0; i < files.length; i++)
fileNames[i] = files[i].getName();
You can use the following code:
String filesDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()+"/YOUR_SPECIFIC_FOLDER/"
File fileList = new FilefilesDirectory
if (fileList != null)
{
File[] filenames = fileList.listFiles();
//Loop through each file and get the file name
String fileNamesArray[] = new String[filenames.length];
int i=0;
for (File tempFile : filenames)
{
fileNamesArray[i] = tempFile.getName();
i++;
}
}

How to list SD card folders and files folders and files programmatically

I'm a newbie in android.
I got an application in which I had list all SD files and folders which include doc,pdf,xls,jpg etc.
How to get Folder Name and File Name of all Folders and files present in my SD card programmatically in android?
I'm using this code but unable to get folder name(root folders and sub folders).
String pdfPattern = ".pdf";
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(pdfPattern))
{
//Do what ever u want mArrayList.add(listFile[i].getName());
Log.v("", "--------- "+listFile[i].getName());
}
}
}
ArrayAdapter songList = new ArrayAdapterthis,android.R.layout.simple_list_item_1,mArrayList);
mlistview.setAdapter(songList);
You should try this sample file explorer.
And this is the method that traverses through the sdcard to fetch the list of files and directories.
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}

How to get all the pictures from the sdcard of emulator and display it in a listView?

I have a folder called images in the sdcard of my emulator. This folder contains pictures clicked from my application. I want to display all the pictures from that folder to a listview. How can i do that? Thanks in advance.
This should get you started with it.
http://android-er.blogspot.com/2010/01/android-file-explorer-with-jpgs-exif_08.html
A simple logic to calculate the requirments,
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
path.add(file.getPath());
if(file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
I haven't tried it, but I suppose if you could be able to create IMageView by putting the images from SD card, you can be able to do this. You can get the images from SD card by creating ContentResolver cursor by passing MediaStore.Images.Media.EXTERNAL_CONTENT_URI and then creating imageview by passing the URI and id (from cursor) to it. After you create ImageView you can create an array of it and pass it to the ArrayAdapter which will then be used to set the data to your ListView. I hope this will help you to solve the problem.
ArrayList<File> imageReader(File root) {
ArrayList<File> a = new ArrayList<>();
File[] files = root.listFiles();
for(int i=0; i < files.length; i++) {
if(files[i].isDirectory()) {
a.addAll(imageReader(files[i]));
}
else {
if(files[i].getName().endsWith(".jpg")) {
a.add(files[i]);
}
}
}
return a;
}
give your root directory as argument, add all the extensions you want to retrieve.

Categories

Resources