Use assets folder images for gallery - android

I am creating an android application.In that one layout file uses a gallery.
I want to know how to use the images from assets folder as gallery images.
for e.g.
for a specific layout file new_am.xml I use the images from assets/new_am folder for the Gallery of that layout.
I know how to use it in case of minimum amount of images , but in my app each folder in assets contains more than 50 images.

You can dynamically access assets folders and set images to Gallery like below,
File fA=new File("file:///android_asset/folderA");
File[] filesA =fA.listFiles();
File fB=new File("file:///android_asset/folderB");
File[] filesB =fB.listFiles();
Also you can collect asset's folders names dynamically.
Hope this will help you.

// to reach asset
AssetManager assetManager = getAssets();
// to get all item in dogs folder.
String[] images = assetManager.list("FolderA");
InputStream inputStream = getAssets().open("FolderA/" + images[0]);
// load image as Drawable
Drawable d = Drawable.createFromStream(inputStream, null);
mImageView.setImageDrawable(d); //set to an Image view
inputStream.close();

Related

how to get directory of images to access via code

I want to add images with my app. I see option to add images in res/drawable folder. But I have lot of images. How can I access the directory?
e.g. to access sdcard location I can use
File directory = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator + AppConstant.PHOTO_ALBUM);
But I have images in drawable folder which will be with app. So how to access directory in android project. So that I can use something like File[] listFiles = directory.listFiles();
Is there better way to save and access images through app?
The solution I can suggest you is to store your images into "Asset" folder in your project and you can easily access all the images at once by following these steps:
Create a folder named "images" in your asset folder.
Copy all your images in that "images" folder.
3.Get your images list like this:
String[] images =getAssets().list("images");
ArrayList<String> listImages = new ArrayList<String>(Arrays.asList(images));
4.Now set the images to your "imageview" like this:
InputStream inputstream=mContext.getAssets().open("images/"
+listImages.get(position));
Drawable drawable = Drawable.createFromStream(inputstream, null);
imageView.setImageDrawable(drawable);
you can access all you images in drawable folder with :
getResources().getDrawable(R.drawable.name_of_drawable)
and also if you want access or load many images , you can use same names ( with postfix / prefix ) and load theme in a loop easily

How to load an SVG file from SD Card into an ImageView?

I'm trying to load an svg file downloaded into sd card with Picasso but it doesn't work.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/secciones.svg");
Picasso.with(context).load(file).into(holder.ivIcon);
I found this question before but he load the file from assets.
Load a vector drawable into imageview from sd card
Is it possible to load the .svg downloaded into the imageView?
You may achieve this by converting SVG to drawable before setting it to ImageView. For this purpose there is a nice library (a bit old and un-managed) Here, however I tried and worked for me. The code is as below :
File dir = Environment.getExternalStorageDirectory();
File yourFile = new File(dir, "your_file_path/filename.svg");
try {
FileInputStream fileInputStream = new FileInputStream(yourFile);
SVG svg = SVGParser.getSVGFromInputStream(fileInputStream);
Drawable drawable = svg.createPictureDrawable();
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
imageView.setImageDrawable(drawable);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
We cannot use SVG with android application. (yes…there are 3rd party libraries that we can use to work with SVG files)
Source : Android — Working with VectorDrawable
VectorDrawable images are simply xml file that contains all information of an image (like lines, curves etc. ) and the color associated with each one of them. The biggest advantage of using vectorDrawable is that we need only one image for different screen devices.This not only reduces the size of the final apk, also it simplifies the maintenance of the project.

What is the path directory to res/drawable type String to pass through File(String path) in Android?

I am trying to get String path directory to images in the res/drawable and filter through it (jpg, png ..)
Something like this
File folder = new File("/res/drawable");
images = folder.listFiles(IMAGE_FILTER);
File image = images[randGen.nextInt(images.length)];
From sd card works easily but couldn't make it from the resource folder! I don't know if this is naive question but I have been trying different options like passing through uri, casting bitmap to File, no luck!
I am trying to get String path directory to images in the res/drawable
Resources are not files on the device. They are entries in the APK's ZIP file. Hence, you cannot construct a File that points to a resource.
You can use AssetManager to list assets, open InputStreams on assets, etc.

Display images in spinner placed in res folder

I have images placed in following path
res>drawable>Images>Currencies>[All images]
I have made a custom spinner Adapter.
viewHolder.flag = (ImageView) view.findViewById(R.id.UICurrencyCurrencyFlag);
viewHolder.flag.setImageDrawable(drawable);
How can i set these images in my ImageView. Images format is png
Best Regards
You have 2 choices:
Put all images in drawable folders without subfolders.
Put all images in asset with your folder structures.
The differences are:
If you go for approach 1, you can retrieve the resource by id. (R.drawable.*)
If you go for approach 2, you get to maintain your folders, but you need to read the resource as raw.
To open file in asset folder, you can refer to a previous post:
Opening a File from assets folder in android
Suggestion:
Rename your file as images_currency_image1.jpg to avoid subfolders but keep unique-ness.
I am not sure if this help in your situation.
You don't really need to make subfolders inside drawable folder. And even if you do, you can't access the contents inside it. And it doesn't even make sense. Why you need subfolders when android is taking care of giving you the drawables by their id. You place your images directly into res/drawable folder like res/drawable/[All images]. And then try to access by R.drawable.UICurrencyCurrencyFlag and not R.id.UICurrencyCurrencyFlag. All the best.
if u place in assets folder u can access in following manner
InputStream bitmap;
try {
bitmap = getAssets().open("icon.png");
Bitmap bit=BitmapFactory.decodeStream(bitmap);
ImageView img = (ImageView) findViewById(R.id.myId);
img.setImageBitmap(bit);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

load Image from specific folder on the sdcard?

I'm attempting to create a gallery/gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is known, ("mnt/sdcard/iWallet/Images") , but in the examples I've seen online I am unsure how or where to specify the path to the pictures folder I want to load images from. I have read through dozens of tutorials, even the HelloGridView tutorial at developer.android.com but those tutorials do not teach me what i am seeking.
Every tutorial I have read so far has either:
A) called the images as a Drawable from the /res folder and put them into an array to be loaded, not using the SDCard at all.
B) Accessed all pictures on the SDCard using the MediaStore but not specifying how to set the path to the folder I want to display images form
or
C) Suggested using BitmapFactory, which I haven't the slightest clue how to use.
If I'm going about this in the wrong way, please let me know and direct me toward the proper method to do what I'm trying to do.
my target android sdk version 1.6...
thanks..
You can directly create Bitmaps from decodeFile (String pathName) that will give you Bitmap object that can be set on ImageView
Update: Below is sudo code with minor errors modify it to suit your needs
File path = new File(Environment.getExternalStorageDirectory(),"iWallet/Images");
if(path.exists())
{
String[] fileNames = path.list();
}
for(int i = 0; i < fileNames .length; i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(path.getPath()+"/"+ fileNames[i]);
///Now set this bitmap on imageview
}
Actually, you are wrong to mention fixed path to access SD-card directory, because in some device it is /mnt/sdcard and in other /sdcard.
so to access root directory of sd-card, use the getExternalStorageDirectory(), it gives you actual path of root directory.
This function will resturn all the files from specific folder you need to pass path till ur folder
public static List getFilesFromDir(File aStartingDir)
{
List result = new ArrayList();
File[] filesAndDirs = aStartingDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
Iterator filesIter = filesDirs.iterator();
File file = null;
while ( filesIter.hasNext() ) {
file = (File)filesIter.next();
result.add(file); //always add, even if directory
if (!file.isFile()) {
//must be a directory
//recursive call!
List deeperList = getFileListing(file);
result.addAll(deeperList);
}
}
Collections.sort(result);
return result;
}
BitmapDrawable d = new BitmapDrawable(getResources(), path+".jpg"); // path is ur resultant //image
img.setImageDrawable(d);
Hope it help u...
You can access your directory using File java class, then iterate through all the files in there, create a bitmap for each file using Bitmapfactory.decodeFile() then add the bitmaps to your gallery.

Categories

Resources