Czy można załadować plik drawable z folderu zasoby?

Czy można załadować drawable z podkatalogu w folderze assets (nie folder drawable)?

Author: Octavian Damiean, 2011-02-03

7 answers

Hope this help:

Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);
 101
Author: Rubycon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-22 19:07:21

Zalecam użycie tego

 Drawable.createFromResourceStream(resources,new TypedValue(), resources.getAssets().open(filename), null)

Który zwraca odpowiednio skalowane drawable dzięki zasobów ...

 6
Author: David,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-05-01 15:32:55

Oto Klasa ze statyczną metodą, aby uzyskać drawable z zasobów. Zamyka również strumień wejściowy.

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;
    }
}
 6
Author: Bart Burg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-11-04 13:24:19

Tak możesz utworzyć obiekt Drawable z InputStreamza pomocą metody createFromStream().

 2
Author: Octavian Damiean,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-03 11:26:08

Oto funkcja, która robi to za Ciebie.

Sprawdź zwracaną zmienną Rysowalną pod kątem null, ponieważ null może zwrócić, jeśli ścieżka jest nieprawidłowa lub istnieje wyjątek 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;
}
 1
Author: Sam J,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-06 18:54:46

To pomogło uzyskać właściwą gęstość

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;
}
 0
Author: richy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-07 11:21:43

W tej wersji nie możesz, jeśli utworzysz podfolder w folderze drawable, nie możesz go użyć w pliku xml, nie zostanie rozpoznany podczas korzystania z android: src.

Spójrz na ten wątek: Czy katalog rysowalny Androida może zawierać podkatalogi?

 -1
Author: SterAllures,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:47:31