Can you load a drawable from a sub directory in the assets
(not the drawable folder) folder?

- 39,405
- 19
- 98
- 102

- 683
- 2
- 11
- 16
8 Answers
Hope this help:
Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);

- 18,156
- 10
- 49
- 70
-
1Just wonder, is the file automically closed for you? For safety, I am currently give a variable the return of open(), then close it manually. – Luke Vo Jun 26 '13 at 19:38
-
@DatVM no it doesn't automatically close the inputstream. I have created a class with static method to get the drawable from assets and added it as separate answer down below. – Bart Burg Nov 04 '15 at 13:23
-
@СергейГрушин for vector drawable please use VectorDrawableCompat.createFromStream(context.assets.open("Cloths/btn_no.xml"), null) – Arsenius Mar 26 '19 at 08:29
I recommend to use this
Drawable.createFromResourceStream(resources,new TypedValue(), resources.getAssets().open(filename), null)
which returns properly scaled drawable thanks to resources ...

- 1,510
- 20
- 16
Here's a class with static method to get the drawable from the assets. It also closes the inputstream.
import android.content.Context;
import android.graphics.drawable.Drawable;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by bartburg on 4-11-2015.
*/
public class AssetsReader {
public static Drawable getDrawableFromAssets(Context context, String url){
Drawable drawable = null;
InputStream inputStream = null;
try {
inputStream = context.getAssets().open(url);
drawable = Drawable.createFromStream(inputStream, null);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return drawable;
}
}

- 4,786
- 7
- 52
- 87
Here is function that does this for you.
Check the returned Drawable variable for null as null may return if the path is invalid or there is an IOException.
public static Drawable getDrawableFromAssetFolder(String fullPath, Activity ctx) {
Drawable d =null;
try {
d = Drawable.createFromStream(ctx.getAssets().open(fullPath), null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return d;
}
Yes you can create a Drawable
object from an InputStream
using the createFromStream() method.

- 39,405
- 19
- 98
- 102
-
is it possible for drawables in drawable folder also?if yes than how? – chikka.anddev Feb 03 '11 at 11:47
-
@chiragshah I don't understand. Images in the drawable directory are converted by the system for you. – Octavian Helm Feb 03 '11 at 11:53
-
I need a quick way to load images from the assets folder as i have a lot of map tiles with same names but different directories. – Jamie Feb 03 '11 at 11:55
-
@Octavien :see this que.:http://stackoverflow.com/questions/4885469/dynamically-show-images-from-resource-drawable – chikka.anddev Feb 03 '11 at 11:59
I was working in a RecyclerView adapter and found that David's answer was not working for me, (for some reason asset.open
remained Unresolved no matter what I imported )
so I found this to work for me (Kotlin code)
val d = Drawable.createFromStream(context?.assets?.open("imageData/${imageName}.png"), null)
here is my directory, as you can see the assets start from the assets folder and here is a link on how to create that assets folder

- 600
- 1
- 7
- 17
This helped getting the right density
private Drawable drawableFromAssetFilename(String filename) {
AssetManager assetManager = mApplicationContext.getAssets();
InputStream inputStream = null;
try {
inputStream = assetManager.open(filename);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
BitmapDrawable drawable = new BitmapDrawable(mApplicationContext.getResources(), bitmap);
return drawable;
}

- 2,716
- 1
- 33
- 42
At this version you can't, if you make a sub folder within your drawable folder you can't use it in your xml file, it won't be recognized when you use android:src.
Take a look at this thread: Can the Android drawable directory contain subdirectories?

- 1
- 1

- 133
- 4
- 13
-
That being the case, what is the easiest way to load a couple of hundred custom map tiles to display on the screen? – Jamie Feb 03 '11 at 10:40
-
Ah my question is about the assets folder, not the drawable. Question edited. – Jamie Feb 03 '11 at 11:18
-