Können Sie ein Zeichen aus einem Unterverzeichnis im Ordner assets
(nicht aus dem Zeichenordner) laden?
Ich hoffe das hilft:
Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);
Ich empfehle, dies zu verwenden
Drawable.createFromResourceStream(resources,new TypedValue(), resources.getAssets().open(filename), null)
die aufgrund von Ressourcen richtig skaliert zurückgegeben wird ...
Hier ist eine Klasse mit einer statischen Methode, um die Assets aus den Assets zu ziehen. Es schließt auch den Eingabestrom.
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;
}
}
Ja, Sie können ein Drawable
-Objekt aus einer InputStream
mithilfe der createFromStream () - Methode erstellen.
Hier ist eine Funktion, die dies für Sie erledigt.
Überprüfen Sie die zurückgegebene Drawable-Variable auf null, da null zurückgegeben werden kann, wenn der Pfad ungültig ist oder eine IOException vorliegt.
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;
}
Dies half, die richtige Dichte zu erhalten
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;
}