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());
}
}
}
}
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 am able to fetch all the images from any specified non hidden folder from device but how can I get all the images from a hidden specified folder.
As soon as I mention my hidden folder name in the query, cursor return null
public static List<MediaData> getAppScannedImages(Context context) {
Cursor imagecursor = null;
List<MediaData> gallerydata = new ArrayList<MediaData>();
try {
final String orderBy = Images.ImageColumns.DATE_TAKEN + " DESC";
imagecursor = context.getContentResolver()
.query(Images.Media.EXTERNAL_CONTENT_URI,
projectionImage,
Images.Media.BUCKET_DISPLAY_NAME + "='"
+ ".myHiddenFolder" + "'", null,
orderBy);
if (imagecursor != null) {
imagecursor.moveToFirst();
int count = imagecursor.getCount();
for (int i = 0; i < count; i++) {
MediaData galData = new MediaData();
galData.setKey_id(i);
galData.setId(imagecursor.getString(0));
galData.setName(imagecursor.getString(1));
galData.setPath(imagecursor.getString(2));
galData.setDate(imagecursor.getString(3));
gallerydata.add(galData);
imagecursor.moveToNext();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (imagecursor != null) {
imagecursor.close();
}
}
return gallerydata;
}
You can try a different approach.
You have to find out the list of hidden folder from sd card and search all those folders for images.
the follwoing code is displays hidden files:
public void goTODir(File dir) {
//dir is initail dir like="/mnt/sdcard"
String imageType = ".jpg";
File[] listFile = dir.listFiles();
if (listFile != null) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
goTODir(listFile[i]);
} else {
if (listFile[i].isHidden()){
if(listFile[i].getName().endsWith(imageType))
{
//add to your array list
}
}
}
}
}
}
String path = Environment.getExternalStorageDirectory().toString();
File dir = new File(path);
File listFile[] = dir.listFiles();
for (int i = 0; i < listFile.length; i++) {
if(listFile[i].getAbsolutePath().contains("your hidden folder name")){
File dirtest = new File(listFile[i].getAbsolutePath());
File listFiletest[] = dirtest.listFiles();
for (int j = 0; j < listFiletest.length; j++) {
get all images from hidden folder
}
}
}
For Kotlin Lover
companion object {
const val FOLDER_PATH = "/YourFolder/.hideen/"
}
/**
* Method to get all Image Path
* #return [ArrayList]
* */
fun getImagePath(): ArrayList<String> {
// image path list
val list: ArrayList<String> = ArrayList()
// fetching file path from storage
val file = File(Environment.getExternalStorageDirectory().toString() + FOLDER_PATH)
val listFile = file.listFiles()
if (listFile != null && listFile.isNullOrEmpty()) {
Arrays.sort(listFile, LastModifiedFileComparator.LASTMODIFIED_REVERSE)
}
if (listFile != null) {
for (imgFile in listFile) {
if (
imgFile.name.endsWith(".jpg")
|| imgFile.name.endsWith(".jpeg")
|| imgFile.name.endsWith(".png")
) {
val model : String = imgFile.absolutePath
list.add(model)
}
}
}
// return imgPath List
return list
}
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.
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.
I am using json to get the image from url and successfully stored into sdcard.
I want to get image from sdcard and display into gridview. when I am using below code I can easily get the all image and easily display into grid view.
public void getFromSdcard() {
ArrayList<String> f = new ArrayList<String>();
File file = new File("/mnt/sdcard/IMG");
if (file.isDirectory()) {
listFile = file.listFiles();
Log.v("Length", "IMG ::" + listFile.length);
for (int i = 0; i < listFile.length; i++) {
f.add(listFile[i].getAbsolutePath());
}
}
}
The problem is I don't want to display all the images into grid view, i want to display specific images (using image prefix).
for example i have list of jpg below
LR-001.jpg
LR-002.jpg
GR-001.jpg
GR-002.jpg
CF-001.jpg
CF-002.jpg
ER-001.jpg
ER-002.jpg
GBR-001.jpg
GBR-002.jpg
GPT-001.jpg
GPT-002.jpg
NCK-001.jpg
NCK-002.jpg
how to get image of GR prefix?
You could check if the filename starts with "GR" for instance:
for (int i = 0; i < listFile.length; i++) {
String fileName = listFile[i].getName();
if (fileName.startsWith("GR"))
f.add(listFile[i].getAbsolutePath());
}
the same with the for-each
for (File file : listFile) {
String fileName = file.getName();
if (fileName.startsWith("GR"))
f.add(file);
}
to get a random entry from your ArrayList you can use the class Random
private Random randomGenerator;
randomGenerator = new Random();
int index = randomGenerator.nextInt(f.size());
File file = f.get(index);
nexInt returns a number between 0 and f.size() - 1
You could check your filenames for the starting letters "GR"
Following Code should work
List<File> f = new ArrayList<File>();
File file = new File("/mnt/sdcard/IMG");
if (file.isDirectory()) {
listFile = file.listFiles();
Log.v("Length", "IMG ::" + listFile.length);
for (File file : listFile) {
if(file.getName().startsWith("GR"){
f.add(file.getAbsolutePath());
}
}
}
Hope it will help you
You could use a FilenameFilter:
f = file.list(new FilenameFilter(){
#Override
public boolean accept(File dir, String filename){
if (filename.startsWith("GR")) return true;
return false;
}
});