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.
Related
I'm trying to click through several full screen images and depending on what button I click, come up with a file name to display next.
For example A_UP0_LEFT2_B
would go to B_UP0_LEFT0_?
the syntax of my file names are currentscreen_xcoordinate_ycoordinate_destinationscreen
The question mark will be another letter and I'm wondering if there is a way for me to look through my drawables directory programatically if I have the string "B_UP0_LEFT0_" and find an image to display next, let's say it's "B_UP0_LEFT0_C.jpg"
short version: if I have a string "B_UP0_LEFT0_" and there is only one file with that string but it also has a "C" at the very end of it, is there any way for me in android to use that string to find the full file from the drawable folder? ie. "R.drawable.B_UP0_LEFT0_C"
You can get all the drawable names via this snippet:
Field[] drawableNames = R.drawable.class.getFields();
for (Field f : drawableNames) {
try {
System.out.println(f.getName());
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
from there I guess a Regex match or simple startsWith() or contains() will do the trick for you.
Also to find a drawable by its name using something like this:
Drawable drawable = getResources().getDrawable(getResources()
.getIdentifier(drawableName, "drawable", getPackageName()));
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();
One part of my current project is downloading images from URL then saving to SDCard. But the problem is all of saved images in sdcard is displayed in android gallery page. What I want is "I don't want all of my saved images in android gallery."
Add ".nomedia" file. Did not work? Try the second option.
Save your files to a folder that starts with ".", e.g., /sdcard/.myimages/
You can create .nomedia file using this code:
String NOMEDIA=".nomedia";
File Folder = new File(Environment.getExternalStorageDirectory() + "/mydir");
if(Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/mydir/"+ NOMEDIA);
if(!nomediaFile.exists()){
nomediaFile.createNewFile();
}
}
I found a simple way(I think so).
Create a folder in 'Android' folder(where data & obb folders are present) and put your media(pics,videos, etc.,) in the folder you've created.
Gallery app ignores that 'Android' folder. Simple :-)
its so late and i know other answers are complete but i think putting .nomedia file makes all pictures in that particular folder hidden :
just change extension of image file programmatically and this way gallery cant detect it as an image . eg if your image is "pic1.jpg" rename it to "pic1.aaa";
and when you want show it again rename it again
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();
}
I am using gridview example with image adapter to render images with difference that these images are retrieved from a particular folder in sdcard for e.g. /sdcard/images.
I am testing this application on emulator.For this i have firstly configured sdcard on emulator and then pushed the images on this particular folder through DDMS under eclipse.
What i want to know is that is it possible to create images folder containing images in sdcard when a user installs the application on the real device and if possible what is the way to do it?
I'm not aware of a way to do it (which is not to say that it cant be done).
One thing that you definitely can do is create the folder when the user first runs the application, and fill it with the images.
You should read the Android Documentation covering the External Storage section.
I think it is possible . you can put all images in a asset folder and when you application will start you can copy it to a particular folder in SD Card. Here is the link for copy the database form asset folder to application . You can try it for copy the images form asset folder to SDCard.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
You can copy images from assets to SDcard
This method copies images from assets folder to your Sdcard .here Jaydeep's Folder is the name of my folder on sdcard.You can use your folder name in that place.
public void copyImagesInSdcard()
{
assetManager = mycontext.getAssets();
assetManager1 = mycontext.getAssets();
System.out.println("In copyImagesInSdcard");
try
{
str1=assetManager.list("");
ss=assetManager1.list(str1[1]);
InputStream is;
//System.out.println(ss[0]);
File file=new File(Environment.getExternalStorageDirectory().getPath() + "/Jaydeep's Folder");
if(file.exists()!=true)
{
file.mkdir();
}
else
{
if(file.length()==0)
{
file.mkdir();
}
System.out.println("Length:"+file.length());
}
for(int i=0;i<ss.length;i++)
{
is=assetManager1.open(str1[1] + "/" + ss[i]);
file=new File(Environment.getExternalStorageDirectory().getPath() + "/Jaydeep's Folder/" + ss[i] );
FileOutputStream out=new FileOutputStream(file);
//Bitmap bi = BitmapDrawable.createFromStream(is,ss[0]);
byte b[] =new byte[4096];
while (is.read(b) != -1) {
out.write(b);
}
out.close();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This can be done by creating zip of all resources and put them in assets folder and then unzip these folder into sdcard using following reference: http://www.jondev.net/articles/Unzipping_Files_with_Android_%28Programmatically%29