Accessing Android DCIM Images - android

How does one obtain images from the /DCIM/100ANDRO folder?
I have tried
File rootsd = Environment.getExternalStorageDirectory();
File dcim = new File(rootsd.getAbsolutePath() + "/DCIM/100ANDRO");
File[] imagelist = dcim.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
}
});
mFiles = new String[imagelist.length];
for(int i= 0 ; i< imagelist.length; i++)
{
mFiles[i] = imagelist[i].getAbsolutePath();
}
mUrls = new Uri[mFiles.length];
for(int i=0; i < mFiles.length; i++)
{
mUrls[i] = Uri.parse(mFiles[i]);
}
but I got a Null Pointer Exception.

Have you remembered to give the app permission to write the external memory?
It is done in the AndroidManifest.xml file, by adding
"android.permission.WRITE_EXTERNAL_STORAGE"
Since you are making a new file object, this is needed, I would guess.

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: how to list files from SD card

I want to list all the files on the SD card. I use a this code for it:
myList = new ArrayList();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( "storage/" + root_sd ) ;
Log.e(myLog, file.getName());
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
Log.e(myLog, list[i].getName() + " : " + list[i].getTotalSpace());
}
And I get a nullPointerException for it after I log the name. I tried an other file also:
file = new File( root_sd ) ;
Ended with the same result.
So, how can I list the files properly? Thx for help!
...//initing the variables
String fileName = Environment.getExternalStorageDirectory().toString();
title.setText(fileName);
ArrayList<String> FilesInFolder = GetFiles(fileName);
mList.setAdapter(new ArrayAdapter<String>(getActivity() , android.R.layout.simple_list_item_1 , FilesInFolder));
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
}
});
and the function
public ArrayList<String> GetFiles(String DirectoryPath) {
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(DirectoryPath);
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<files.length; i++)
MyFiles.add(files[i].getName());
}
return MyFiles;
}
If you want a different layout make an own ArrayAdapter

Get Image from sdcard using Prefix?

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;
}
});

ArrayList<File> contains filename

I have a problem with an ArrayList (File in the above code). This arraylist is composed by files that are located into the sd. The problem is that I can have duplicates (the same image, but in different paths into the sd, so the same filename but different path) and I want to remove them. So I use this code:
ArrayList<File> removedDuplicates = new ArrayList<File>();
for (int i = 0; i < File.size(); i++) {
if (!removedDuplicates.contains(File.get(i))) {
removedDuplicates.add(File.get(i));
}
}
But it doesn't work, I guess because contains() for a List of File looks at the filepath instead of at the filename. Is it true? How can I solve my problem? I also tried with:
ArrayList<File> removedDuplicates = new ArrayList<File>();
for (int i = 0; i < File.size(); i++) {
if (!removedDuplicates.contains(File.get(i).getName())) {
removedDuplicates.add(File.get(i));
}
}
but still it doesn't work. Thanks
The type of getName is String and the type of object in your ArrayList is File, so you're never going to get the same thing.
You want to compare the names inside the ArrayList.
for(File f : files){
String fName = f.getName();
boolean found = false;
for(File f2 : removedDuplicates){
if(f2.getName().equals(fName)){
found = true;
break;
}
}
if(!found){
removedDuplicates.add(f);
}
}
Its very simple.
PS: Tested Code
Map<String, File> removedDuplicatesMap = new HashMap<String, File>();
for (int i = 0; i < files.size(); i++) {
String filePath = files.get(i).getAbsolutePath();
String filename = filePath.substring(filePath.lastIndexOf(System
.getProperty("file.separator")));
removedDuplicatesMap.put(filename, files.get(i));
}
ArrayList<File> removedDuplicates = new ArrayList<File>(
removedDuplicatesMap.values());
Try this:
ArrayList<File> removedDuplicates = new ArrayList<File>();
File temp;
for (int i = 0; i < File.size(); i++) {
temp=new File(File.get(i).getAbsolutePath());
if (!removedDuplicates.contains(temp)) {
removedDuplicates.add(File.get(i));
}
}

Filtering files in a directory on Android

In my app i am getting the images from a folder in gallery and saving it into an array list.Now i want to extract only the files with .jpg extension.How can i do it
The code for saving to array list is
private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCard
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
tFileList.add(file.getPath());
}
return tFileList;
}
You can use FilenameFilter interface to Filter the Files.
Change your codeline
File[] files=f.listFiles();
as below:
File[] jpgfiles = f.listFiles(new FileFilter() {
#Override
public boolean accept(File file)
{
return (file.getPath().endsWith(".jpg")||file.getPath().endsWith(".jpeg"));
}
});
Use .endsWith() method from Java String Class to check File Extension from file path.
Method:
public boolean endsWith(String suffix)
Your Code something like,
private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCard
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
String filePath = file.getPath();
if(filePath.endsWith(".jpg")) // Condition to check .jpg file extension
tFileList.add(filePath);
}
return tFileList;
}
private List<String> ReadSDCard()
{
String extension = "";
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
int ind = files[i].getPath().lastIndexOf('.');
if (ind > 0) {
extension = files[i].getPath().substring(i+1);// this is the extension
if(extension.equals("jpg"))
{
tFileList.add(file.getPath());
}
}
}
return tFileList;
}
In your code add the following:
String filePath = file.getPath();
if(filePath.endsWith(".jpg"))
tFileList.add(filePath);

Categories

Resources