Here I am trying to get pdf and csv files from WhatsApp Documents folder but not getting any instead of printing only Sent and Private folders only but I want all files from WhatsApp Documents
Please help
File srcFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/media/"+"com.whatsapp" + "/WhatsApp/Media/WhatsApp Documents/");
File[] allFiles = srcFile.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String filename) {
System.out.println("filename="+filename);
return (filename.toLowerCase().endsWith(".pdf") && filename.toLowerCase().endsWith(".csv"));
}
});
list all files from WhatsApp Documents folder
Related
I am trying to program my own file manager for android. What I want to know is that how can I read the file names of all the files present in external/internal storage?
I want to read the file names and show them in a listview so the user could see what files are present in what folder. I know this thing is going to work in recursive manner as I have to read the content of sub directories as well.
This is what you have to do. Before going to write please refer the File class in java. This will help you to clear lot of things.
Below is the snippet that provides the list of files.
File directory = new File(path);
File[] listFiles = directory.listFiles();
if (listFiles != null) {
for (File file : listFiles) {
if (file.isDirectory())
// do the stuff what you need
else if (file.isFile()) {
// do the stuff what you need
}
}
}
}
Following code will give you list of files in android sdcard:
/**
* Return list of files from path. <FileName, FilePath>
*
* #param path - The path to directory with images
* #return Files name and path all files in a directory, that have ext = "jpeg", "jpg","png", "bmp", "gif"
*/
private List<String> getListOfFiles(String path) {
File files = new File(path);
FileFilter filter = new FileFilter() {
private final List<String> exts = Arrays.asList("jpeg", "jpg",
"png", "bmp", "gif");
#Override
public boolean accept(File pathname) {
String ext;
String path = pathname.getPath();
ext = path.substring(path.lastIndexOf(".") + 1);
return exts.contains(ext);
}
};
final File [] filesFound = files.listFiles(filter);
List<String> list = new ArrayList<String>();
if (filesFound != null && filesFound.length > 0) {
for (File file : filesFound) {
list.add(file.getName());
}
}
return list;
}
You can call same method to get sub directory file also.
Am am trying to work with this tutorial here which loads every image on the device into a grid view, but im not sure how to modify it to just display images from one particular directory in external storage.
Get the folder content with folder.listFiles() and create an Uri from every entry.
String folderName="Photos";
ArrayList<Uri> images = new ArrayList<Uri>();
File folder = new File(Environment.getExternalStorageDirectory() + File.separator
+ folderName);
File[] files = folder.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String filename) {
return filename.endsWith(".jpg") || filename.endsWith(".png");
}
});
for (File file : files) {
images.add(Uri.parse(file.getAbsolutePath()));
}
Then you could use the array list with uris to pass it to the adapter.
I have been trying to make a music player application,and as I have just started android, Could any one tell me how can I accces the music files in my sdcard, I have given the location of the sdcard as "\sdcard\" in my String variable MEDIA_PATH ,but what after that? How should the system access the music files put it more simply the files ending with .mp3 format,I don't know which function to use. I'd appreciate your suggestions, thanks.
You can get all the mp3 files from rooted sdcard by the following code.
File home = Environment.getExternalStorageDirectory();
if (home.listFiles( new Mp3Filter()).length > 0) {
for (File file : home.listFiles( new Mp3Filter())) {
songs.add(file.getAbsolutePath());
}
ArrayAdapter<String> songList = new ArrayAdapter<String>
(this,R.layout.song_item,songs);
setListAdapter(songList);
}
class Mp3Filter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".mp3"));
}
}
Here songs is a ArrayList which stores the actual path of the song.
Use FileFilter for scanning the .mp3 files from sdcard. like:
//Check media mounted or not
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
File sdCardDir=new File("/sdcard");
File sdCardDir=Environment.getExternalStorageDirectory();
ArrayList<File>files=getFiles(file);
private ArrayList<File> getFiles(Filefile){
File files[]=sdCardDir.listFiles(filter);
for(File f:files){
if(f.isDirectory()&&f.canRead()){
getFiles(f);
}else if(f.isFile()){
videlList.add(f);
}
}
//USE FileFilter
FileFilter filter=new FileFilter(){
#Override
public boolean accept(File f){
returnf.isDirectory()||f.getName().matches("^.*?//.(mp3)$");
}
}
I'm trying to make an app that can take images from a directory on the android phone and display them in a layout. I seem to be working my way towards a solution in a backwards manner. I know to use view.setBackgroundDrawable(Drawable.createFromPath(String pathName)); to display the image, but I don't know how to get the image's path from the directory.
I have a vague idea of what to do, but would appreciate clarification on this matter. I think the next step is to use:
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
File file[] = Environment.getExternalStorageDirectory().listFiles();
}
But how do I filter the files so that only image files get stored in my file[]? Such as .png or .jpg/.jpeg files? And after that, should I use files[].getPath or files[].getAbsolutePath? I would store the result in a String[].
What I am mainly asking for is verification that the above code should work. And also, how I might filter to store only image files such as .png, .jpg and .jpeg.
Thank you for your time.
You want to do something like this for filtering:
File[] file = folder.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String filename) {
return filename.contains(".png");
}
});
If you want the filepath of the image of lets say the file at file[0], you would do this:
file[0].getAbsolutePath();
You want to implement a FileFilter and pass it to listFiles. You can create one that filters out only image files as you specified.
EDIT: and you want to use getAbsolutePath() as the argument to createFromPath.
Here is code for only to get .Png ,.jpg files and containing floders
private File[] listValidFiles(File file) {
return file.listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String filename) {
File file2 = new File(dir, filename);
return (filename.contains(".png") || filename.contains(".jpg") || file2
.isDirectory())
&& !file2.isHidden()
&& !filename.startsWith(".");
}
});
}
I have an application that needs to read images from a folder created by the application on the sdcard(sdcard/"foldername"/"filename.jpg". I have no idea what the names of the files are because the user specifies the names of the files. I need to read the images from the folder and make something like the default image viewer. Im thinking read them into a grid view first but 1) cant figure out how to dynamically read them from a folder 2) how would I implement the image options like the default viewer? If there was a way to open the default viewer on a certain folder that would help.
any input would be amazing been working on it for a while.
Thanks
Here's how you can get a list of folders off of the memory card:
String state = Environment.getExternalStorageState();
if(state.contentEquals(Environment.MEDIA_MOUNTED) || state.contentEquals(Environment.MEDIA_MOUNTED_READ_ONLY))
{
String homeDir = Environment.getExternalStorageDirectory();
File file = new File(homeDir);
File[] directories = file.listFiles();
}
else
{
Log.v("Error", "External Storage Unaccessible: " + state);
}
This code is from the top of my head, so some syntax may be off a bit, but the general idea should work. You can use something like this to filter down the folders to only folders that contain images:
FileFilter filterForImageFolders = new FileFilter()
{
public boolean accept(File folder)
{
try
{
//Checking only directories, since we are checking for files within
//a directory
if(folder.isDirectory())
{
File[] listOfFiles = folder.listFiles();
if (listOfFiles == null) return false;
//For each file in the directory...
for (File file : listOfFiles)
{
//Check if the extension is one of the supported filetypes
//imageExtensions is a String[] containing image filetypes (e.g. "png")
for (String ext : imageExtensions)
{
if (file.getName().endsWith("." + ext)) return true;
}
}
}
return false;
}
catch (SecurityException e)
{
Log.v("debug", "Access Denied");
return false;
}
}
};
Then, change the first example to:
File[] directories = file.listFiles(filterForImageFolders);
That should return only directories that contain images. Hopefully this helps some!