I am trying to load vector drawables from the assets.
It works fine when I use png files:
final String name = "image.png";
final InputStream stream = context.getAssets().open(name);
drawable = Drawable.createFromStream(stream, name);
stream.close();
I tried using VectorDrawable.createFromStream() and VectorDrawableCompat.createFromStream() but both always return null.
When I add the vector drawable to the drawbles resources it can be loaded without a problem.
I also tried using the binary xml file which gets created when adding the vector file to the drawables resources.
I use API level 24 and Support libs 24.2.0 with Android 5.1.1 to test this. It should work on Android 4.2 and later.
Does anyone know how to fix this?
This issue could relate to this one:
Load a vector drawable into imageview from sd card
After looking at the Android source code I found a solution.
The method createFromStream does not seem to support vector drawables. Because it uses a BitmapFactory.
but we can load them as xml resource:
final XmlResourceParser parser = context.getAssets().openXmlResourceParser("assets/folder/image.xml");
drawable = VectorDrawableCompat.createFromXml(context.getResources(), parser);
The image.xml musst be a precompiled binary xml file.
You can extract them out of the generated apk file when you add the vector drawable to the drawable resources.
The problem with this approach is, that it needs Android 5. But I need a working code for Android 4.2. Anyone an idea?
Related
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
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.
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 );
I've tried all solutions. Finally decided to post this question.
The situation is as follows:
My drawable has test.png image which I use in my project.
In eclipse it works perfectly but in Android Studio it doesn't work.
Also I'm not getting any compiler error.
I am getting only this type of error almost 54 times when cradle build. As this image is set of as background to 54 screens. :(
What could be the problem?
You should remove the extension part.
For example if your image is called mimg.png in your
resource files you should access it via #drawable/mimg.
Edit
If in eclipse is fine, then create the project new and add file by file. I also had this issues and after some tries ai gave up and created new and after added file by file.
As an alternative please check if you add it in other drawables folders.
That's it.
You need to reference your drawable without the extension: Like this:
android:src="#drawable/test"
Remove the image extension from your xml file and try that please!
I need to get a file descriptor for a dae file, so I can get the Start Offset and Length. I have got file descriptors for png files, which work fine. I did this by using this code.
int id = 0;
id = getResources().getIdentifier("duck", "drawable", getPackageName());
mAssetDescriptor = getResources().openRawResourceFd(id);
Like I said, for my png's this worked fine, for my dae's, however, this did not work, I got the following exception:
android.content.res.Resources$NotFoundException:
File res/drawable/duck.dae from
drawable resource ID #0x7f020030
mAssetDescriptor was null.
So I tried to do another way. I put my dae files in the assets folder
mAssetDescriptor = getAssets().openFd("duck.dae");
This came up with the error
This file can not be opened as a file
descriptor; it is probably compressed.
What I would like to know, is how I can get the Start Offset and Length of the file without using the getFileDescriptor() and getStartOffset() functions, as it seems I can not use these.
I guess it is also worth nothing the dae files are all under 1mb, with duck.dae which i'm trying to load at the moment at 500k.
Thanks alot for the help
Tom
This probably works for pngs because they are not additionally compressed, while most other files are. See this thread: http://groups.google.com/group/android-ndk/browse_thread/thread/8274a4c51cc9cc1d
Try adding and extension to your files, that prevents file from being compressed.