Android add multiple pictures to drawable res folder - android

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 );

Related

Overview over all vector images in drawables

Does Android Studio (or perhaps some other application/method of viewing) support viewing all the vector images in the drawables folder so that you have an overview over them all?
My issue is that I have hundreds of vector images in there, and I don't know anymore if I've imported certain icons or not and in order to find out I need to either go through them individually or search (guess) them by name.
Is there a simpler and quicker way? Some plugin perhaps ?!
AFAIK, there is no Android Studio plugin to preview vectors massively, but you could create your own "previewer activity" temporarily to retrieve all the desired folder (drawable folder or a dedicated asset folder for all your vectors).
Maybe something like this would work (I didn't test it, and probably vectors will need to be handled in a more specific way, yet it could lead you the right path):
AssetManager mgr = context.getAssets();
String[] files = mgr.list("images");
InputStream ist = null;
ArrayList<Drawable> vectorDrawables = new ArrayList<>();
for (String file : files) {
Drawable d = Drawable.createFromStream(mgr.open(file), null);
vectorDrawables.add(d);
}
Then put the list into a grid view or something you prefer to get a quick visualization of all your vectors.

How i can get a drawable res, if only i get the image from a url

I have a External Library and this need a Int, the int is the resource
banners.add(new DrawableBanner(intResource));
But i only have the image in a url and i did this
Glide.with(MyActivity.this).using(new FirebaseImageLoader()).load(storageReference).into(mPhotoProfile);
Drawable d = mFotoPerfi.getDrawable();
This gives me the Drawable but it does not allow me to place, it needs the resource in the form of "Integer" and not "Drawable"
How can I somehow pass it to the "Resources" and get it there?
You can't. A photo from Firebase is not a resource. Resources are packaged in your APK, from the res/ directory in your project.

How to accès images from drawable folder?

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;

how to use images present in drawable-hdpi,drawable-mdpi,drawable-ldpi of res folder?

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

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