List all the files from a specific folder in android - android

I tried this one : How to access all mp3 files from all the subfolders in the sdcard?
I need to list all .amr files from a folder (ABC) and on click of any list item, do something.
Thanks in advance.

You can try this:
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File f = new File(path);
File file[] = f.listFiles();
Log.d("Files", "Size: "+ file.length);
for (int i=0; i < file.length; i++)
{
//here populate your listview
Log.d("Files", "FileName:" + file[i].getName());
}
This will list all the files in the pictures folder. Use the path to your folder to get the files in that folder
Dont forget to use the following permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Related

How can I list files in a directory in Android?

I have a folder in root with some files I want to list but the array returns null:
String path = getenv("EXTERNAL_STORAGE") + "/Files";
File directory = new File(path);
File[] files = directory.listFiles();
Permissions are granted
First off, you need to use Environment.getExternalStorageDirectory() instead of getenv():
String path = Environment.getExternalStorageDirectory().toString() + "/Files";
File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
Log.d("Files", "FileName:" + files[i].getName());
But a very important note is the /storage/emulated/0 folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored. That's why your array returns null. You'll need to find the actual physical location on your device where it is stored.

Android Download Manager is duplicating existing files

I am trying to download Audio files into one of the directories owned by the application using DownloadManager.
Here is my code:
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_MUSIC),
audioName + ".mp3");
if (file.exists()) {
return;
} else {
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE).setAllowedOverMetered(true)
.setAllowedOverRoaming(true).setVisibleInDownloadsUi(false)
.setDestinationUri(Uri.fromFile(file));
mgr.enqueue(request);
}
The problem is that downloadManager is duplicating already downloaded audios, for exemple if I have an audio file named "test.mp3" I would find both "test.mp3" and "test1.mp3" in my application directory.
Am I doing something wrong?
Please keep in mind that I want to download files in the app directory and this is why I am using setDestinationUri
I had the same issue. Up to date what I have realized is that the download manager rarely stops downloading and will keep doing so and appending values to the same file name. So I suggest going through your destination directory and deleting duplicate files. You can call this method onCreate.
private void cleanUp()
{
String path = "PATH TO YOUR DOWNLOAD DIRECTORY/FOLDER NAME/";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
for(int j=1;j<files.length;j++)
{
if(files[i].getName().contains("-"+String.valueOf(j)+".mp3")) // format according to how your download manager has renamed the file e.g "-"+String.valueOf(j)+".mp3" for fileName-1.mp3 or String.valueOf(j)+".mp3" for fileName1.mp3
{
toast(files[i].getName());
files[i].delete();
}
}
}
}

Android Development EXTSDCARD naming on different devices?

I have a variable that just states outright:
File file = new File("/Extsdcard")
Am I foolish enough to think all android devices mount an external devices in this wording?
or should I be using Environment.getExternalStorageDirectory().getAbsolutePath();
My application searches through the first three folder layers so this is quite important to the functionality.
String path = Environment.getExternalStorageDirectory().toString()+"/YourDirectory";
Log.d("Files", "enter code herePath: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
//It gives you list of all directoris/files list
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < 3; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
Environment.getExternalStorageDirectory() gets the top level of the external drive.

get the list of files and folders exist in root directory

in my program I need something like file manager to list the files and directories of internal and external memories . for example see the picture below...
Example of what I want
how can I get the root directory
in fact I want to get a list like picture above that include internal and external directory .
thanks
Getting the list of files and folders exist in root directory, like this:
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}

Delete files from application directory folder in Android?

I created folder inside application directory.
File dir = new File(getActivity().getFilesDir().getParent() + File.separator + "Image directory");
in that again i created folder for specify contents
File dir1 = new File(getActivity().getFilesDir().getParent() + File.separator + "Image directory" + File.separator+ "Image 1");
Inside that I am going to storage images. Images are stored and accessed.
I want to delete all images which are stored at dir1 location.
I tried
if(dir1.isDirectory())
{
for (int j = 0; j < dir1.length; j++)
{
File file = new File(dir1, dir1[j]);
//file.canExecute();
file.delete();
}
}
file.delete(); returns false each time.
I added permissions in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
In your file.delete method you have used bundle don't know where did that came from.
Below code will delete all files in your Image Directory
File dir1 = new File(getActivity().getFilesDir().getParent() + File.separator + "Image directory" + File.separator+ "Image 1");
if (dir1 .isDirectory()) {
String[] children = dir1 .list();
for (int i = 0; i < children.length; i++) {
new File(dir1 , children[i]).delete();
}
}
Make sure you have permission for writing in external memory, which you already have
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

Categories

Resources