Is there a way to get all the icons inside the drawable folder? Im looking for a way similar to how one would get all files from a folder on a SD card, like:
ArrayList<File> file = new ArrayList<File>();
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"directoryName");
With that snipped you get all files inside a folder called directoryName which is located inside the phones pictures folder.
So i was hoping for something like this:
Drawable[] d = getResources().getDrawables() //But this way you could only get one drawable through its id
All the files in the assets folder can be accessed so easily so why note drawables:
String[] files = null;
AssetManager assetManager = getActivity().getAssets();
try {
files = assetManager.list("BookFrames");
} catch (IOException e) {
e.printStackTrace();
}
I could store the pictures in the assets folder and get all of them with the above snippet, however, i was hoping to create an additional res folder where i would store all my image buttons, and other pictures that i use in my app. The advantage of this is that i could store pictures with different resolutions, and hopefully android would return the correct sized image for a certain screen resolution.
The current scenario. I am creating a custom adapter which includes pictures of book covers, and to prevent the pictures looking tiny on certain screens i would need to include many resolutions of the covers. And by using the drawable directory i let android take care of which resolutions to display, there fore i need a way to get all the drawables as a list when populating my adapter (hoping that android would choose the appropriate picture resolution for me).
I saw this tread How to display list of resource drawables
which show a method to do this, but with that method it would get all the pictures (i dont think android will choose the appropriate resulution with that method, it will just collect all pictures).
Imagine making a memory game where each card is a picture, to make that look good on different screens you need many resolutions for those pictures. So placing those pictures inside drawable would let the android engine take care of the choosing. And you would not need code to check users screen size. But maybe its better to use the below and check the screen resolution of the used than choose appropriate asset folder?
assets/pictures/xhdpi/frame1
hdpi/frame1
Related
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
I am working on an a simple android application that stores objects in an array, and displays them to the user via a listview. Each object contains a photo and a single text field. For demonstration purposes, I would like to pre-populate this array with some hardcoded objects.
The trouble is that the images are typically acquired through the camera interface, and each object only stores the path to that image. I can add the hardcoded images as drawables, but then they don't have a file path. I could when the app is initialized, convert the drawables to bitmaps and save the bitmaps to the SD card, but that seems too complicated to be the correct answer...
Any ideas on the best way to get these images into file storage so that I refer to them via their URIs?
Thanks!
When you create your views in your adapter, have a ImageView in your layout and use a loader or custom AsyncTask to load the image in the background and have it update the ImageView instance once it has the data.
check the background from this on loading images from the network.
You acquire images from the camera but notice that when you do a resource chooser where mimetype = img/*, the selector just merges local camera(gallery) storage with other photo, content providers. An example of common chooser for photos is in 'Evernote' where you go to the composer view with the 2X2 grid and touch the 'attachment' icon... thats a photo chooser...
In other words , it helps to understand the general practice for managing photos and for presenting them in imageViews.
Typically, there is a lazyLoader that has an interface with args for the imgView and the URI of the image source. Under the covers on a call to 'loadImage(view, uri), the implementation checks the following:
is the bitmap already in a memCache?
the local file , that backs the bitmap, does it exist in the folder reserved for this purpose?
if none of above, go get the array of bytes for the img from across the network ( or in your case , get bytes from camera ).
Even though your question is a little different , IMO , the standard practices for images may apply and you may want to adapt one of the many libs for image presentation to your specific requirements.
I need to change the source of an ImageView dynamically. I have a whole bunch of them with the same names, stored in their different dpi directories (drawable-hdpi etc) under res/. For this I've been using ImageView's setImageResource() and passing it a value from an array of resource IDs I've created at runtime like so:
decorations = new int[]{
R.drawable.bird1,
R.drawable.flower2,
R.drawable.bird3,
..etc};
Anyway, for some reason, the 6th image gets corrupted into an alternative image used for other things, not one in the resources list.
What on earth is going on?
Turns out I had a corrupt/incorrect image or the image was in the wrong directory or something. Still, strange that it decided to just display the next image in the directory rather than just crashing with an Exception or something.
I fill the ListView from DataBase by using SimpleCursorAdapter where I am overriding getView method. By this method for each item I am loading the small image from my telephone.
Where do you suggest to store my small images (20 images x 1 kB)?
in DB (it is on my SDCARD);
in the folder on SDCARD;
in assets;
in raw folder.
Images that you are including with your project should go in one of the drawable folders, not assets or raw, for instance yourProjectFolder/res/drawable-mdpi/
You can provide higher / lower resolution images in the ldpi, and hdpi folders.
As to whether you should use the drawables folder or some form of SD storage it comes down to whether or not you may ever need to change the images. If you include them in your project drawable folder then you will be unable to make any changes to them after compile time. If you store them on the SD then you have the option to go back and download different / more pictures to use.
So it just depends on if you'll ever need to the option to add more, or change the images that you're using.
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");