How can I populate the image switcher dynamically, when I have the images in the drawable folder and sub folder .. like drawable-ldpi/S_001/a001_001.jpg.
I have tried to populate one image, but the application crashed.
Here is the code:
ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setImageDrawable(getResources().getDrawable(getResources().getIdentifier("S_001/a001_001.jpg", "drawable", getPackageName())));
I need some assisstance.
drawables folder is for img resources used by your app, like icons, 9-patches, backgrounds and other ui elements. You can't put subdirectories there.
If you plan to use some photos in an ImageSwitcher or anything like this, you should use assets folder. Read this to find out more.
Related
I'm going to develop an application which shows a ListView which each row have a seperate image (like personal picture for contacts list). It seems I have two options:
1- Store all images in Asset Folder and load image using setImageDrawable() command.
2- Store all images in Drawable Folder and load theme using setImageResource(R.drawable.xxx)
So my questions is does they differ in performance? And how can I speed up listView rendering such that images displayed in acceptable speed.
There should not much performance different to access image from Drawable or asset folder. You can see the answer too - > Android: Accessing images from assets/drawable folders
But, When you using ListView you are recreating the view . So, Where ever you store the image all the images are will not feel much different .
There are not so much differences. Just for coding pattern changes.
assets/
Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource.
drawable/
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.
https://developer.android.com/guide/topics/resources/providing-resources.html#ResourceTypes
I wanted to Display Calendar Icon as a Button In Android. I have created a Image Button as,
ImageButton DtPk = new ImageButton(this);
I know that image will set as,
Drawable d = Drawable.createFromPath("#drawable/calendar");
DtPk.setBackgroundDrawable(d);
But where do i store image in my project? And how.
When the project was created you should see a folder called "res" and inside it several different drawable folders. Place the image inside of the one called drawable-mdpi. Then simple do the following:
DtPk.setImageResource(R.drawable.calendar);
Here is another SO post explaining more about drawable folders: Drawable folders in res folder?
And read the section called Bitmap File in this document: http://developer.android.com/guide/topics/resources/drawable-resource.html
I am a beginner developer and I am a little confused about drawables and bitmaps. From my current understanding:
-Bitmaps are like empty images (without a type yet?)
-Drawables are images that I can draw on them
What I want to do in the first place is to open a red image I made and then draw on it. How can I do that?
Also, all drawables must be placed in the 3 drawable folders, right?
By the way, what's the different of "assets" and "res" folder?
Thank you in advance.
A drawable might be a bitmap, but maybe a shape
drawable is more general
assets holds file data
res holds (xml) defined data
Just some answers to your bunch of questions
I have an array of files contained in the sdcard: i would like to show an item in a ImageSwitcher but could not find the SetImageBitmap but only SetImageResource, but i have not these in res folder.
How can i do it?
use this below code instead of setImageBitmap. I think you have files in array.
imgSwitcher.setImageURI(Uri.fromFile(filename));
I am new to Android development. To implement a photo gallery app, tutorial said to add images to res/drawable and access them via R.drawable.image1 .. etc. But there is no drawable folder default in eclipse, then I created a one and add images. But cannot access images, there is no option in R.drawable. as "image1"
Any idea ?
you should create "drawable" folder under "res", and you'd best place png image files in that folder. When accessing these image files programmingly.set an image for an ImageView for example:
Drawable imgDrawable=getResources().getDrawable(R.drawable.yourImageFileName));
imgView.setImageDrawable(imgDrawable);
Sometimes the drawable folder is not created by default Instead it will have "drawable-hdpi","drawable-mdpi" and "drawable-ldpi".These folders are for placing images for various screen densities to support different resolutions.Your "drawable folder" is for baseline medium density.
In your case create a folder named "drawable" under "res" and place your image inside that folder and you will be able to access it using R.id.imagename .Try refreshing the project and check whether you have created the drawable folder under the res folder.Also check the case of the folder name that you have created.
I faced same problem , but when i added those images again in jpg format it worked.
Might work for you too.
My System was based on windows 10 & was using android studio 4