Overview over all vector images in drawables - android

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.

Related

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

VectorDrawable createFromStream does not seem to work

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?

How to load Textures with Unity3d on Android

i am trying to get this really easy setup to work, and it works on my computer but not on Android.
After trying it for the whole day im now asking since i cant find a single example on the internet.
The setup:
I have a lot of *.png files in a folder. Since the problem is with android i put them into a "StreamingAssets" folder
I have a RawImage called "Overlay"
I have a Dropdown called "Dropdown"
What should happen:
1. The Dropdown_script checks the StreamingAssets folder for all *.png files and adds every Filename to the dropdown options menu.
2. Once the user chooses one of the dropdown values my "Overlay" image changes its texture to the according file.
Pretty easy on PC ... impossible on android(with unity3d) as it seems.
My Dropdown Script:
public class DDScript : MonoBehaviour {
void Start () {
GameObject obj = GameObject.Find("Dropdown");
//Get all filenames
var info = new DirectoryInfo(Application.streamingAssetsPath);
var fileInfo = info.GetFiles("*.png");
//Clear the DropDown Menu Items from the Inspector
obj.GetComponent<Dropdown>().options.Clear();
//Make the first chosen item the first filename
obj.GetComponent<Dropdown>().captionText.text = fileInfo[0].Name.Substring(0, fileInfo[0].Name.IndexOf("."));
foreach (var file in fileInfo)
{
Dropdown.OptionData list = new Dropdown.OptionData(file.Name.Substring(0, file.Name.IndexOf(".")));
obj.GetComponent<Dropdown>().options.Add(list);
}
}
Its not working. It is working on my Computer but it is just not working on Android. I know that under Android everything gets packed into a jar file and there is nothing like a folderstructure. StreamingAssets should still have the files but i can understand that this part maybe cant work. Which would not be that bad, since it just finds the names and puts it into an array, i could do that within the program by hand. Since i know from beforehand how much and which files will be in the folder and it wont change ever.
This is my Change Texture Method
It is invoked by the Dropdown OnValueChange so the "newOverlay" is simply the int from the Dropdown. For debugging purposes i set the filepath to a specific file. later it will change depending on newOverlay
public IENumerator SetOverlay(int newOverlay)
{
GameObject obj = GameObject.Find("Overlay");
img = (RawImage)obj.GetComponent<RawImage>();
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "testfile.png");
WWW www = new WWW(filePath);
yield return www;
img.texture = www.texture;
}
And i do not understand why this is not working!
The Documentation reads:
" This means that if you do not use Unity’s WWW class to retrieve the file then you will need to use additional software"
So using www is the right way. But it is not working!
No Texture gets loaded. i dont get the red questionmark, my image is just plain white. Please can someone help me here? Maybe i just cant see the forest due to all of the trees or the day was too long.
I really just cant believe that it is this hard to load a bunch of images that are within my apk file wiht unity :(
Thank you very much in advance
Edit: Solution (somehow)
First of all the problem was that System.IO.Path.Combine adds a back-slash between file and folder, but not between the folders themself!!!!
public void SetOverlay(int newOverlay)
{
GameObject obj = GameObject.Find("Overlay");
img = (RawImage)obj.GetComponent<RawImage>();
string filePath = Application.streamingAssetsPath + "/testfile.png";
WWW www = new WWW(filePath);
while (!www.isDone)
{}
img.texture = www.texture;
GameObject.Find("DebugText").GetComponent<Text>().text = filePath;
}
Havent found a solution to the Filename problem yet but im glad that this stupid "bug" is resolved
Have you tried putting the png files into the drawable folder and accessing them with BitmapFactory.decodeResources()?

Image files under src root folder not present in android build (Codename ONE)

It is similar to this question, however my problem is that the images are there in Codename ONE simulator (image can be loaded and shows correctly), but when I do an android build and try it on my phone, I get a java.io.FileNotFoundException. According to the comment given by Shai Almog to the question mentioned above, it should work.
What is wrong here? Could it be filenames have to be all lower case or all upper case?
(I am omitting the source here because it seems to be a problem with packaging rather than the code itself.)
UPDATE: I tried changing all filenames to lowercase but to no avail.
Finally I found out that you have to open the files as resources (Thanks to #ChrisStratton for pointing in the right direction whilst the comment was obviously about android SDK development).
This works:
private static Image loadImage(String path) throws IOException {
InputStream in=null;
try {
// works in simulator but not on device:
// in = FileSystemStorage.getInstance().openInputStream(path);
// works on both simulator and device:
in = Display.getInstance().getResourceAsStream(null, path);
return Image.createImage(in);
} finally {
if (in!=null) {
in.close();
}
}
}
You should create Images with EncodedImage.create("/fileName") or better yet add them to the res file where you can use the multi image format: http://www.codenameone.com/how-do-i---fetch-an-image-from-the-resource-file---add-a-multiimage.html
The approach with the input stream is slightly less efficient than what we can do internally.
Files in the src directory are implicitly placed in the assets directory for Android in Codename One so there is no need for an assets directory.

how to access resources in a android library project

I am building an android library project which need some static resources(images/xml and etc) internally.
Then I wonder where I can put these resources and how to access them?
Since I put the resources in the assets folder. And I use the AssetManager to access the resources:
public class DataProvider {
byte[] getDrawableData(int num) {
AssetManager am = Resources.getSystem().getAssets();
InputStream is = null;
try {
is = am.open("t.jpg");
} catch (IOException e) {
return "".getBytes();
}
if (is != null) {
Drawable dw = Drawable.createFromStream(is, null);
Bitmap bm = ((BitmapDrawable) dw).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
return stream.toByteArray();
}
return "".getBytes();
}
}
However I got the error: W/System.err(19583): java.io.FileNotFoundException: t.jpg
What is the problem?
As Sankar V said the library project does not support the raw assets. Then It seems that I have to put the resurces under the res folder.
But I have tried to use the Resources.getSystem().getResources().... method which can not get what I want. And people said that this method can only get the system level resources rather than the application level resources.
If this is the truth. Does it mean that I have to hold a reference of the Context object anywhere I want to get any resources?
Then I wonder where I can put these resources and how to access them?
From Android Doc's
Library projects cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.
Docs Reference Link : LibraryProjects
Solution : How to access resource files saved in res/ directory of library project from application project
Note : If you want to access the resources from Library Project itself add the resources to res/ directory not in assets. Because the files in assets won't be bundled with the .jar file.
You can access it as usual using the reference generated in R.java file.
Ex: To access a image from drawable use
R.drawable.image_name
Does it mean that I have to hold a reference of the Context object anywhere I want to get any resources?
Really you should hold a reference to context to access the resources
Update:
Now Android library project can be generated as aar (Android Archive) file which allows you to bundle the resource files needed for the library project instead of the old jar (Java Archive) file which only allow you to bundle Java classes.
Or you can use a Linked Folder in Eclipse, and link your assets folder across to your new project.
Okay, its been a while since a working solution has been given to this question. I have been working with creating Android library for few days, and encountered this problem myself as well. I tried all the above solutions put forward, but none of them worked for me.
Recently I watched a video on Android library, and got a glimpse of the solution, even though it was not mentioned that it is necessary for using res directory resources inside library code.
So the solution is simple.
Simply, you can check if following line
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourpackage"> <!-- this line -->
</manifest>
is present in your manifest. Otherwise you won't be able to use any of the strings, styles, drawables in your library java files/xml files.
I have checked this on my end, and found this solution randomly, after searching online for nearly 2 days.
Try it and tell me if it works.

Categories

Resources