get images' names in directory - android

How can I get only images' names in known directory.
Trying use this (from here)
File sdCardRoot = Environment.getExternalStorageDirectory();
File yourDir = new File(sdCardRoot, "yourpath");
for (File f : yourDir.listFiles()) {
if (f.isFile())
String name = f.getName();
// make something with the name
}
but don't works.
How can I do this?

private ArrayList<File> fileList = new ArrayList<File>();
//getting SDcard root path
File root = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath());
getfile(root);
for (int i = 0; i < fileList.size(); i++) {
// getting file name
System.out.println(fileList.get(i).getName());
if (fileList.get(i).isDirectory()) {
                teSystem.out.println("This is Directory not an image");
            }
}
// getfile(file) method
public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
fileList.add(listFile[i]);
getfile(listFile[i]);
} else {
if (listFile[i].getName().endsWith(".png")
|| listFile[i].getName().endsWith(".jpg")
|| listFile[i].getName().endsWith(".jpeg")
|| listFile[i].getName().endsWith(".gif"))
{
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}
hope it will help your purpose.

Related

How to get all file list in my storage?

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.

Android clearing app cache clears provider also

I used following code to delete my app cache.
public void clearApplicationData() {
File cacheDirectory = getCacheDir();
File applicationDirectory = new File(cacheDirectory.getParent());
if (applicationDirectory.exists()) {
String[] fileNames = applicationDirectory.list();
for (String fileName : fileNames) {
if (!fileName.equals("lib")) {
deleteFile(new File(applicationDirectory, fileName));
}
}
}
}
public static boolean deleteFile(File file) {
boolean deletedAll = true;
if (file != null) {
if (file.isDirectory()) {
String[] children = file.list();
for (int i = 0; i < children.length; i++) {
deletedAll = deleteFile(new File(file, children[i])) && deletedAll;
}
} else {
deletedAll = file.delete();
}
}
return deletedAll;
}
Once I delete the code means it deletes the provider which I declared in manifest. Is there any way to clear cache without deleting content provider?
You can avoid this by not deleting database folder
if (!fileName.equals("lib")&&!fileName.equals("files")&&!fileName.equals("database")) {
deleteFile(new File(applicationDirectory, fileName));
}

I want to delete data on backpress of activity in android

on back press of activity delete data this code work but appication is crash
public void onBackPressed() {
File dir = context.getCacheDir();
if(dir!= null && dir.isDirectory()){
File[] children = dir.listFiles();
if (children == null) {
// Either dir does not exist or is not a directory
} else {
File temp;
for (int i = 0; i < children.length; i++) {
temp = children[i];
temp.delete();
}
}}}
Thanks in advance.

Delete only .jpg files from folder in android

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 to Search Images from SDCard?

I have done an Application which shows the images which are stored in drawable folder.
Now I want to implement an application which Searches images from SDcard/Phone Memory and display them in Thumbnail.
try this with some Modification it may help u..
private void walkdir(File dir)
{
String filePattent = ".jpge";
String filePattentCAP = ".JPGE";
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(filePattent) || listFile[i].getName().endsWith(filePattentCAP))
{
fileName.add(listFile[i].getName());
filePath.add(listFile[i].getAbsolutePath());
}
}
}
}

Categories

Resources