Android studio fileFilter with assetManager - android

I have the following functionality to a desktop application with java and I want to do the same thing for a mobile application.
File f = new File(pathToFiles);
File[] files = f.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return (pathname.getName().endsWith(".img") ? true : false);
}
});
Now in my android class I access the same files stored in a folder inside assets using the AssetManager with the following way:
AssetManager assetManager = context.getAssets();
String[] files = assetManager.list("folderInsideAssets");
List<String> it = new LinkedList<String>(Arrays.asList(files));
The problem here is that I get them as string in a list instead of a File class like in plain java.
Is a way to convert it to a File[] class and proceed with the same way or I should handle this differently?
Thank you in advance!

Stackoverflow Question on loading pictures from asset
Yes, create another folder in assets directory. use getAssets().list(<folder_name>) for getting all file names from assets:
String[] images =getAssets().list("images");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
Now to set image in imageview you first need to get bitmap using image name from assets :
InputStream inputstream=mContext.getAssets().open("images/"
+listImages.get(position));
Drawable drawable = Drawable.createFromStream(inputstream, null);
imageView.setImageDrawable(drawable);

Related

Read folder name and folder's file name from assets folder

I have this file structure in my android project:
I want to read all the folder names and their file names contained in assets folder. I have tried so many things but I am not able to achieve my goal. Can somebody guide me to correct direction?
Thanks in advance.
You can read the filenames of a particular folder from assets as follows :
private List<String> getFileNames(Context context, String folderName) throws IOException {
AssetManager assetManager = context.getAssets();
String[] files = assetManager.list(folderName);
return Arrays.asList(files);
}
Simple read all the assets using the below function
public myClass(Context myContext) {
AssetManager mngr = myContext.getAssets();
InputStream is = mngr.open("assests.txt");
}
A simple getAssets().list("") will list all folders.
And files if there are. But you havenot.

Getting path of local image instead of URL Android

I am working with gif images at the moment and have followed the answer from the question here
From this I have the following line of code:
GifWebView view = new GifWebView(this, "http://cdn.lifebuzz.com/images/17487/lifebuzz-35df9a5ee351ae72711e5a293658e319-limit_2000.gif");
Now, I want to download this image, save it locally in the "drawable" folder or "assets" folder and then access it from there.
I have tried downloading the image into the "drawable" folder and trying :
GifWebView view = new GifWebView(this, "#drawable/myimg.gif");
I have also tried this:
int imageResource = R.drawable.myimg;
Drawable image = getResources().getDrawable(imageResource);
String imageUri = "drawable://" + R.drawable.myimg;
GifWebView view = new GifWebView(this, imageUri);
Still no luck.
I've tried accessing it from the assets folder but this seemed even more complicated.
I just would like to save it locally and access it locally, I don't really care where it is stored.
Thanks for any help.
EDIT
public GifWebView(Context context, String path) {
super(context);
loadUrl(path);
}

How to get path of any file from my my application data folder

My application files are bellow, I want to get sharethis.png file in java code , What is syntax ?
I used this ...
Bitmap bMap = BitmapFactory.decodeFile("file:///android_asset/www/sharethis.png");
but not working
Try:
Bitmap shareThis = BitmapFactory.decodeResource(context.getResources(),
R.drawable.shareThis);
If you want to reference it from a JavaScript or html file in your assets folder, use file:///android_asset/www/sharethis.jpg.
Otherwise, according to this answer, this will list all the files in the assets folder:
AssetManager assetManager = getAssets();
String[] files = assetManager.list("");
This to open a certain file:
InputStream input = assetManager.open(assetName);
Try this:
try {
// get input stream
InputStream ims = getAssets().open("sharethis.png");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
Log.e("I/O ERROR","Failed")
}
Instead of using the assets dir, put the file into /res/raw , and you can then access it using the following URI: android.resource://com.your.packagename/" + R.raw.sharethis

How to get the name of all image files in a folder?

I want to get the names of all image file in a directory (lets say pictures) into an array of strings. I'm still new so I don't know how to approach this. I just need a way to retrieve the filenames with the .png extension from the pictures folder on the sd card so I can store it in an array.
this is how to list files under any path.
private void listAllFiles(String pathName){
File file = new File(pathName);
File[] files = file.listFiles();
if(files != null){
for(File f : files){ // loop and print all file
String fileName = f.getName(); // this is file name
}
}
}
You can do this using the java.io.File
If you just want the names you can use.
File dir = new File("<YourPath>");
ArrayList<String> names = new ArrayList<String>(Arrays.asList(dir.list()));
If you want the whole file object use.
File dir = new File("<YourPath>");
ArrayList<File> files = new ArrayList<File>(Arrays.asList(dir.listFiles()));
More Information
java.io.File

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