Can you load a drawable from a sub directory in the assets
(not the drawable folder) folder?
你可以从资产(而不是drawable文件夹)文件夹中的子目录加载drawable吗?
7 个解决方案
#1
100
Hope this help:
希望这个帮助:
Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);
#2
5
I recommend to use this
我建议使用这个
Drawable.createFromResourceStream(resources,new TypedValue(), resources.getAssets().open(filename), null)
which returns properly scaled drawable thanks to resources ...
由于资源,它返回适当缩放的drawable ...
#3
5
Here's a class with static method to get the drawable from the assets. It also closes the inputstream.
这是一个使用静态方法从类中获取drawable的类。它还会关闭输入流。
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
2
Yes you can create a Drawable
object from an InputStream
using the createFromStream() method.
是的,您可以使用createFromStream()方法从InputStream创建Drawable对象。
#5
1
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.
检查返回的Drawable变量是否为null,因为如果路径无效或存在IOException,则可能返回null。
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;
}
#6
0
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;
}
#7
-1
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.
在这个版本你不能,如果你在drawable文件夹中创建一个子文件夹,你不能在你的xml文件中使用它,当你使用android:src时将无法识别它。
Take a look at this thread: Can the Android drawable directory contain subdirectories?
看看这个帖子:Android drawable目录可以包含子目录吗?
#1
100
Hope this help:
希望这个帮助:
Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);
#2
5
I recommend to use this
我建议使用这个
Drawable.createFromResourceStream(resources,new TypedValue(), resources.getAssets().open(filename), null)
which returns properly scaled drawable thanks to resources ...
由于资源,它返回适当缩放的drawable ...
#3
5
Here's a class with static method to get the drawable from the assets. It also closes the inputstream.
这是一个使用静态方法从类中获取drawable的类。它还会关闭输入流。
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
2
Yes you can create a Drawable
object from an InputStream
using the createFromStream() method.
是的,您可以使用createFromStream()方法从InputStream创建Drawable对象。
#5
1
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.
检查返回的Drawable变量是否为null,因为如果路径无效或存在IOException,则可能返回null。
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;
}
#6
0
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;
}
#7
-1
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.
在这个版本你不能,如果你在drawable文件夹中创建一个子文件夹,你不能在你的xml文件中使用它,当你使用android:src时将无法识别它。
Take a look at this thread: Can the Android drawable directory contain subdirectories?
看看这个帖子:Android drawable目录可以包含子目录吗?