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();
}
Related
I am trying to read images from Assets.In Assets i have been using a-z folders and these folders contains images with the starting name of folder like if there is folder named YImages
then images in this folder image's names are also starting with Y word.
I am using this function to get images from Assets:
try {
pics = Drawable
.createFromStream(
getAssets().open(
imageFolder + wordString.get(index)
+ ".jpg"), null);
picture.setBackgroundDrawable(pics);
picture.refreshDrawableState();
} catch (IOException e) {
e.printStackTrace();
}
Theres a space at your image path. java.io.FileNotFoundException: yImages/ Year.jpg
Try trimming the File name.
Just change all the image file name with the small letters do not use the capital letter in image file name . i.e badger_thumbnail.jpg,badge_thumbnail.jpg,year.jpg like this. Also keep in mind that there is no space in your any image name.
I'm developing an application for android, the application shows a list of activities of a carnival. I want that the application has the images of every activiy to avoid the user has to download it when runs the application, and improve user experience's with the application.
To do it I have added the images for every activity as resource, but I don't know if this is the best way to do It. There are some limitation with Resource that I don't like for example you can't create subfolders in drawable folder to organize the images, the only characters accept as a resource name are [a-zA-Z0-9_], and others....
Is there any good way to do it? or I have to use resources if I want to avoid the user has to download the images.
Thanks
You can download images when user load your application first time then save it to sdcard.
When your application runs again check weather images available in sdcard or not.
If not then download images else use images from sdcard.
The android system is designed to have all of your drawable items (aka images) in the resources folder. If you want the images to be packaged with your app (rather than downloaded after the fact) they need to be included in res/drawable
Its always better to have the images downloaded but if your requirement is that the application should have all the pictures beforehand then you can use the "assets" folder where I suppose there is no restriction with respect to the names of the pics. I have tried the following code and it works:
ImageView img = (ImageView)findViewById(R.id.activity_image);
try {
Drawable d = Drawable.createFromStream(getAssets().open("#ahd ka.png"), null);
img.setImageDrawable(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I have purposely used a png file with special characters in name just to show that the program runs. Hope this helps.
I am able to keep the images of different resolutions inside drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder but do not know how to access the images from assets/www folder . Kindly help.
you need to do any thing more to handle multi-resolution.just get the image by id and system will pic automatically as per current resolution.
http://developer.android.com/guide/practices/screens_support.html#range
You can get an image id by (no matter of dpi):
R.drawable.your_image
So, with id you can do everything that you need (use search).
If you want to use the path try the following :
private void showImage() {
String uri = "drawable/icon";
// int imageResource = R.drawable.icon;
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView imageView = (ImageView) findViewById(R.id.myImageView);
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
But it's recommended to use the R references :
int imageResource = R.drawable.icon;
Drawable image = getResources().getDrawable(imageResource);
Source
drawable-hdpi drawable-mdpi,drawable-ldpi are just different folders to make difference between images quality depending on the screen resolution. The idea is to put the same images in different folder with different resolutions ...
You don't need the AssetManager. You can do
BitmapFactory.decodeFile("file:///android_asset/www/bullet.jpg")
Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.
Update: Explanation The BitmapFactory has a method to decode a file via the decodeFile method. That's point one. Android lets you access a file in the assets folder via the file:///android_asset/{path} path. In your case, the Image at /image/Malay/bullet.jpg is the assets folder can be accessed via the the file:///android_asset/www/bullet.jpg
I have a bunch of images that are common for all resolutions (currently I only support one resolution. There are reasons for this).
My question is, when is it appropriate to store images in the assets folder rather than the drawable folder?
The difference between a raw resource in res/raw directory and an asset is that assets behave like a file system, they can be listed, iterated over, discovered, just like files while for raw resources, you need the resource ID.
From: http://mylifewithandroid.blogspot.it/2009/06/assets.html (emphasis mine)
Also you may use assets when you need to organize things using sub-folders.
you can change the files in assets folder anytime you want and nothing will be effected (i mean the resources of your application by "nothing"). it may be useful to give the name of the image file as a parameter for example.
If you want to access images by their original name, you better save images in the assets folder. Then, you can get them by using:
AssetManager:AssetManager am = getResources().getAssets();
try {
InputStream is = am.open("image.png");
// use the input stream as you need
} catch (IOException e) {
e.printStackTrace();
}
I Hope this helps.
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.