Android: display every image from a specific directory - android

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.

Related

List all files from WhatsApp documents folder android 12 programmatically

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

How create Custom gallery by reading all images stored in android internal storage

I am developing an android app in which i am saving images in a specified folder after some editing.Now i want to show all these stored images in my activity. I m able to show a particular image by specifying a particular name.But i want to show all images together.. Thank in advance.
I m using Following code to show single image......
File myDir=new File("/sdcard/MyCollection");
myDir.mkdirs();
try {
File f=new File(myDir, "My Image name.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
ImageView img=(ImageView)findViewById(R.id.imageView);
img.setImageBitmap(b);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
if i follow your question, than you can retrieve the list of file with .jpg extension run a loop on list and show in imageview.
File root = new File("/sdcard/MyCollection");
final String files[] = root.list(imageFilter);
FilenameFilter imageFilter = new FilenameFilter() {
File f;
public boolean accept(File dir, String name) {
if(name.endsWith(".jpg")) {
return true;
}
f = new File(dir.getAbsolutePath()+"/"+name);
return f.isDirectory();
}
};
files will return an array of files.You can loop on it.

how to browse only text files from my application

i have built an application,where i am supposed to browse only for txt files in the device and read it.but i am not getting a valid solution to it.Like for example if we take whatsapp,there you can browse different files under different headings like under videos you will only browse videos and under gallery you will only browse images.but whats in case for text files only?is there any way?Please suggest.
try this.
//for example your folder present in sdcard/cts.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cts");
for (File wavfile : f.listFiles(new FileExtensionFilter()))
{
String str = wavfile.getName().toString();
}
//create a class and the filter based on your need file extension
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".txt") || name.endsWith(".TXT") );
}
}
thank you.
I would put your directories your searching into arrays... Loop the arrays and look for the file extensions after the " . " if its not a text extension remove it. Then display the new array to user like the photo gallery works or however else you would want to represent them.
Note- If you need access to these later you may want to be saving the path if the user say...needs to click and open them...
you may be able to use something to this effect for searching the SD Card
File file[] = Environment.getExternalStorageDirectory().listFiles();
file[i].getAbsolutePath();
Then search it for the extensions you want...
This will give you a file manager as you like. Just needs a bit of work around. But definitely helps
//fill method is to get the dir
private void fill(File f)
{
File[] dirs = f.listFiles();
this.setTitle("Current Dir: " + f.getName());
List<Option> dir = new ArrayList<Option>();
List<Option> fls = new ArrayList<Option>();
try
{
for (File ff : dirs)
{
if (ff.isDirectory())
{
dir.add(new Option(ff.getName(), "Folder", ff.getAbsolutePath()));
}
else
{
String filetype[] = objOption.getName().split("\\.");
if (filetype[(filetype.length) - 1].equals("txt")) {
fls.add(new Option(ff.getName(), "File Size: "+ ff.length()+" bytes", ff.getAbsolutePath()));
}
}
}
}
catch (Exception e)
{}
Collections.sort(dir);
Collections.sort(fls);
dir.addAll(fls);
if (!f.getName().equalsIgnoreCase("sdcard"))
{
dir.add(0, new Option("..", "Parent Directory", f.getParent()));
}
adapter = new FileArrayAdapter(FileChooser.this, R.layout.dialog_file_view,dir);
lvFileList.setAdapter(adapter);
}

Create a custom album in the Android Gallery App

I want create a new 'album' in Android. I unserstood that you have to do it with your own ContentProvider. But i can't figure out how. If you take pictures, all the images appear in the album 'Camera'. If you have installed Google+ you'll see 2 extra albums: Instant Upload & Profile Picture.
We want to create something similar. We have an album per user online and we want that it appears as an album item in the Gallery app. Can someone point me in the right direction.
I already checked: http://developer.android.com/guide/topics/providers/content-providers.html
Actually im not really sure if it is possible.
You can use this way to create album in Gallery app. The name appears as 'app images'.
String path = Environment.getExternalStorageDirectory().toString();
File dir = new File(path, "/appname/media/app images/");
if (!dir.isDirectory()) {
dir.mkdirs();
}
File file = new File(dir, filename + ".jpg");
String imagePath = file.getAbsolutePath();
//scan the image so show up in album
MediaScannerConnection.scanFile(this,
new String[] { imagePath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
if(Config.LOG_DEBUG_ENABLED) {
Log.d(Config.LOGTAG, "scanned : " + path);
}
}
});

Android: How to get image files from directory into view.setBackgroundDrawable()

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(".");
}
});
}

Categories

Resources