I'm developing an application which has some assets bundled with it - they are mostly web pages with associated graphics, css etc which I'll display in a custom WebView. I'm trying to work out how to serve up different assets to different devices without massive duplication.
I'd like to replicate the res folder, but for assets. For example:
assets/html/page1.html references assets/html/example_pic.png but I want to serve up a different example_pic.png depending on screen density or screen size.
Ideally I'd like to have something like assets_hdpi/html/example_pic.png and assets_mdpi/html/example_pic.png
Is there an elegant way of achieving this? Can I somehow utilise the /res/ folder management to the same end result by putting the example_pic.png in to /res/drawable_hdpi etc and then somehow pointing the webpage to the drawable?
A possible workaround:
Prepare a 'subpath' named string for the different resolutions you want to address, e.g.:
res/values-hdpi/subpath.xml: <string name="subpath">hdpi</string>
res/values-ldpi/subpath.xml: <string name="subpath">ldpi</string>
Prepare corresponding subpaths in your asset folder, e.g.:
assets/hdpi, assets/ldpi and store your assets there.
Finally, use getResources.getString(R.string.subpath) to get the best asset subpath.
String AssetPath= "file:///android_asset/"+getResources.getString(R.string.subpath)+"/";
myWebView.loadPage(AssetPath+"index.html");
Related
I have a lot of images and I want to classify them in the drawable folder in Android Studio for easy access, is it possible?
Not possible. The only way to sort your drawable resources is by renaming them, usually with prefixes (i.e. ic_ for icons).
You can't put them in folders. However, you can control the name of them to find them easier. For example, if I have buttons coded with shapes, etc, I will name these button_name. This way, it all of my buttons are there for me to view. If I have a few minutes in a certain activity, I will do something like results_, and then all of my results images are there. Hopefully they makes sense and helps.
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
Is there any possibility to refer to wrongly named images in Android? I have bunch of images that have wrong names, for example IMG123456.jpg (u. c.), 1_img.jpg (number first) etc. It would be really hard to rename them all, cause I have JSON file that I load and there are also these names.
So my questions are:
Is it possible to store these image files anywhere?
Is it possible to refer to these images with Glide library?
I realized that assets folder should have path like this: src\main\assets. To refer to images with Glide in assets folder it's needed to do that like this:
.load("file:///android_asset/images/1_img.jpg")
It's very important to remember about the extension!
Does the .apk format handle hardlinks? Or does it simply copy the same file over and over?
I've some a simple test and it seems like hardlinks are not handled, meaning that the size of the generated .apk increases significantly when you have multiple hardlinks to the same file. Is there a way to make the resources point to the same file instead?
The use case is the following: I have several images, which are used in some places in my application and their names contain some information about them. For example I could have an apple.jpg, apple_red.jpg, apple_red_big.jpg, apple_big.jpg, apple_green.jpg etc. My application uses the image that matches the data the most, and thus if the data is about a red, big apple it will use apple_red_big.jpg, while if the application only knows that the data regards apples it will simply use apple.jpg.
At the moment I'm not providing a different file for each resource, so apple_red.jpg is simply an hardlink to apple.jpg. In the future I might decide to add more images and thus apple_red.jpg could have its own image.
Also, since apple.jpg is used as a fallback option, it will pretty much always be a simple hardlink to an other image which happen to be also apple_something.jpg.
How can I avoid duplicating all these images in the final apk?
If you provide the same image under different file names (even if they are just hard links), the aapt will package them up as separate resources. Rather than using the file system to create hard links, the Android way of doing what you want is to provide alias resources. The technique is described here in the docs. Basically, you simply create a bitmap drawable that references another drawable:
file res/drawable/apple.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/apple_something" />
where you store apple_something.png in the res/drawable directory. The alias resource takes up just a tiny bit of additional room in the .apk file.
In code, both R.drawable.apple and R.drawable.apple_something would then retrieve the same image (although they would be different Drawable objects); in xml it would be #drawable/apple and #drawable/apple_something.
First I thought that I could find this list on the net, but I'm either looking for the wrong term or such list doesn't exist.
What I need is basically a cheat sheet of all predefined resource folders in an Android project. For example, a list could say something like this
res/drawable - all graphics go here
res/drawable-hdpi - all graphics of higher resolution go here
res/layout - some-meaningful-description
res/values - some-meaningful-description
res/layout-land - some-meaningful-description
etc.
I am really surprised that such list isn't easily found on the net. Whenever I need to add some resource I haven't used before, I have to look on the net for the correct naming (and I would rather look at the list of res folders).
Taken from here:
In the /res folder you can have:
animator/ -XML files that define property animations.
anim/ - XML files that define tween
animations
color/ - XML files that define a state list of colors.
drawable/ - Bitmap files / Nine-Patches (re-sizable bitmaps)
/ State lists / Shapes / Animation drawables / Other drawables
layout/ - XML files that define a user interface layout.
menu/ - XML files that define application menus, such as an Options
Menu, Context Menu, or Sub Menu.
raw/ - Arbitrary files to save in their raw form.
values/ - XML files that contain simple values, such as
strings, integers, and colors.
arrays.xml for resource arrays (typed arrays).
colors.xml for color values
dimens.xml for dimension values.
strings.xml for string values.
styles.xml for styles.
xml/ - Arbitrary XML files
Also see Accessing Alternative Resources for more specific device configurations (locale, dpi, size, aspect, orientation, etc)
Never thought about it, but you are right.
You can always create a new project based on available samples projects in eclipse and see they have chosen to organize it.
You can see how one of my project is organized.
my project in eclipse
It should be noted that these folder layouts are not written in stone. One can create a folder tree in whatever manner one chooses. This is just the default tree created with IDEs such as Eclipse and is, as a result, the most popular and often consistent format. If you find another format to fit you better then you are free to use it. Just reference properly in your code.