How to accès images from drawable folder? - android

Im trying to create gallery application. In tutorial I've found, I need to do get images using R.drawable.image_name. I've created folder drawable under /res folder, copied my images one by one into that folder, al of them are .png. But when I try to access them by writing R.drawable.image I can't do that because there is no images I've copied. How to fix it?

Clear and rebuild your project. If you got automatic builds disabled, your IDE will not refresh R class contents and won't autocomplete your assets IDs.

Do clean build or if you are using android studio do invalidate cache and make project afterwards.
For Accessing Drawable:
Drawable drawable = this.getResources().getDrawable(R.drawable.image_name);
If you want bitmap :
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.image_name);
if you want to access by url :
imgUrl= "drawable://" + R.drawable.image_name;

Related

How to read SVG file for ImageView from assets folder Android Studio [duplicate]

I want to show a vector image (say vectorimage.xml) in an imageview from the sd card. Please throw some insight on how to do this in android.
What I have tried already :-
String imagePath = Environment.getExternalStorageDirectory() + "/ folder/productImage.xml";
bitmapImage = BitmapFactory.decodeFile(imagePath);
Drawable bgrImageDrawable = new BitmapDrawable(bitmapImage);
The above code snippet does not work since the bitmapImage is coming as null.
The BitmapFactory can't load vector drawables.
You have to use the VectorDrawable or VectorDrawableCompat class.
To load a vector drawable you need to use a xml loader.
Some parser like the one for the resources need a precompiled binary xml file. You can find them in the apk file when you put the vector drawable in the drawable resource directory.
Here is a sample to load it from the assets, you should be able to use a similar code for the loading from the sd card.
final XmlResourceParser parser = context.getAssets().openXmlResourceParser("assets/folder/image.xml");
drawable = VectorDrawableCompat.createFromXml(context.getResources(), parser);
This way needs at least Android 5.0

Android add multiple pictures to drawable res folder

I want to add over 200 icons (smaller than 50x50px) to the drawable resource. The drag and drop does not work for me. Using New Image asset permits to upload only a single photo.
here is my project resource folder structure:
After adding pictures to the drawable folder, I was unable to get the images. Here is my code
Resources res = activity.getResources();
String mDrawableName = user.getNat().toLowerCase() + ".png";
int resourceId = res.getIdentifier(mDrawableName , "drawable", activity.getPackageName());
Drawable drawable = res.getDrawable(resourceId);
countryIcon.setImageDrawable(drawable );
Why I cannot find images in drawables?
Where should I copy the photos in the file explorer/finder? I have drawable folder and mipmap folders for different dimensions
Is it a good idea to have 200 pngs inside the project? (I have different codes like 33483477, and for every code I have a image which I need to display)
Just copy them directly to your project under <your project>/<your module>/src/main/res/drawable[-qualifier].
Without other details of your project, I can't say specifically into which drawable resource folder these should live (-hdpi, -nodpi?).
Is it a good idea to have 200 pngs inside the project?
It depends on the size. There is an APK size limit of 100MiB. If bigger, you can stage expansion files, which are essentially opaque archives of data.
Other than that, you should consider the usage pattern of the images. Are all of the images viewed, always, by all users? Point being, if the user has to download all of them eventually, bundling them all in the APK is probably okay. If they only use a few of them, making them download them all (in your APK) is a waste of their bandwidth.
Staging the images on a server and downloading them on demand adds a little complexity to your app, but it's a well-established pattern.
I use below code to get drawables. hope it will work for you. no need of .png file extention. thats what you did wrong. Also use getResources() method before calling getIdentifier.
String mDrawableName = user.getNat().toLowerCase(); //image or icon name. not need extention .png
int resourceId = getResources().getIdentifier(mDrawableName, "drawable", getPackageName()); // or use context.getResources(). If you use fragments, use getActivity().
Drawable drawable = getResources().getDrawable(resourceId);
countryIcon.setImageDrawable(drawable );

Adding new image to Xamarin Android changes all other images

I'm new to Xamarin and I'm facing strange issue that whenever i try to add new image to Resources/drawable folder of Android and referring it to shared code,all other images are getting change and starts referring random images.
Steps which I'm following :
Add a new Image to Resources/drawable folder.
Change Build Action to "AndroidResources".
And referring the new image in Source attribute.
thanks in advance.
Looks like your Android resource generated file was not re-generated or broken. Try Clean-Build Android project. If that doesn't help, delete bin and obj folders and try building again.

Load image file from ClassLoader in android

I have a trouble with load image file in android project. I need to load from it because I have some private reasons.
here is my android project structure:
ProjectAA
----assets
----image
--------ic_next.png
----src
--------HomeActivity.java
----AndroidManifest.xml
I tried to load ic_next.png in HomeActivity.class with:
InputStream is = getClass().getClassLoader().getResourceAsStream("image/ic_next.png");
Bitmap bm = BitmapFactory.decodeStream(is);
but bm always return null. I also add "image" folder to java build path
I searched google but I can't find any solution. please help me.
Thanks
Put image folder under src folder or include image folder as source folder in project settings.

How to load image for Android ImageView from classpath?

I have an Android App which contains an external dependend jar library. This external library contains some png-bitmaps which I want to show on some ImageViews in my application.
Looking inside the apk file of my application I can find the images in a sub directory ("aq\img") as expected.
What is the best way to show an image (insight the classpath) on a ImageView.
I tried this
ImageView imgView01 = (ImageView) findViewById(R.id.imageView1);
Drawable d = Drawable.createFromPath("aq/img/sample.png");
imgView01.setImageDrawable(d);
and this
InputStream is = ClassLoader
.getSystemResourceAsStream("sample.png");
Bitmap bm = BitmapFactory.decodeStream(is);
imgView01.setImageBitmap(bm);
but it did not work (I did not really expect it to be that simple).
The image remains empty.
Thanks for help!
Regards Klaus
The PNG image is not a "system resource" because a system resource on Android is a file in the regular filesystem. The image that you want is within the APK archive. You would be able to use getSystemResourceAsStream to open an InputStream for reading the bytes of the APK archive, but not a file within the archive unless you implemented the logic to extract the file yourself.
Simply use this:
InputStream is = getClass().getClassLoader().getResourceAsStream("aq/img/sample.png");
EDIT: Note that on Android, the name that is passed to getSystemResourceAsStream is treated as a path to the file relative to root (/). Because the APKs are stored in /data/app, then to open an InputStream for reading the APK, you can use ClassLoader.getSystemResourceAsStream("data/app/PACKAGE-1.apk"), where "PACKAGE" is the package of your app.

Categories

Resources