是否可以从资源文件夹中加载可绘制对象?

发布于 2024-10-15 12:25:15 字数 52 浏览 6 评论 0原文

您可以从assets(不是drawable文件夹)文件夹中的子目录加载drawable吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

梦里兽 2024-10-22 12:25:15

希望这有帮助:

Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);

Hope this help:

Drawable d = Drawable.createFromStream(getAssets().open("Cloths/btn_no.png"), null);
○闲身 2024-10-22 12:25:15

我建议使用它,

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

由于资源的原因,它会返回正确缩放的可绘制对象......

I recommend to use this

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

which returns properly scaled drawable thanks to resources ...

一身仙ぐ女味 2024-10-22 12:25:15

这是一个带有静态方法的类,用于从资源中获取可绘制对象。它还关闭输入流。

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;
    }
}

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;
    }
}
只是偏爱你 2024-10-22 12:25:15

是的,您可以使用 createFromStream() 方法。

Yes you can create a Drawable object from an InputStream using the createFromStream() method.

喜爱纠缠 2024-10-22 12:25:15

这是为您执行此操作的函数。

检查返回的 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;
}

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;
}
旧情别恋 2024-10-22 12:25:15

我正在 RecyclerView 适配器中工作,发现 David's 的答案对我不起作用,(由于某种原因,无论我导入什么,asset.open 仍然无法解决)

,所以我发现这对我有用(Kotlin 代码)

val d = Drawable.createFromStream(context?.assets?.open("imageData/${imageName}.png"), null)

这里是我的目录,你可以看到资产从资产文件夹开始,这里是 链接,了解如何创建该资源文件夹

在此处输入图像描述

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

enter image description here

冷心人i 2024-10-22 12:25:15

这有助于获得正确的密度

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;
}

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;
}
獨角戲 2024-10-22 12:25:15

用于从资产中获取可绘制对象的 Kotlin 函数

感谢 @Bart Burg,

我已将 Java 函数改编为 Kotlin,用于从资产文件夹中检索 Drawable。 Kotlin 版本利用该语言的标准库函数来实现高效、安全的资源管理。这是 Kotlin 的实现:

import android.content.Context
import android.graphics.drawable.Drawable
import java.io.IOException

object AssetsReader {

    /**
     * Gets a Drawable from the assets folder.
     * 
     * @param context The context of the application.
     * @param url The path of the drawable in the assets folder.
     * @return Drawable The drawable from the specified path, or null if an error occurs.
     */
    fun getDrawableFromAssets(context: Context, url: String): Drawable? {
        return try {
            // Automatically closes the InputStream after the operation is done
            context.assets.open(url).use { inputStream ->
                // Create a Drawable from the InputStream
                Drawable.createFromStream(inputStream, null)
            }
        } catch (e: IOException) {
            // Log the exception and return null if an error occurs
            e.printStackTrace()
            null
        }
    }
}

Kotlin Function to Get Drawable from Assets

Thanks to @Bart Burg

I've adapted a Java function to Kotlin for retrieving a Drawable from the assets folder. The Kotlin version utilizes the language's standard library functions for efficient and safe resource management. Here's the Kotlin implementation:

import android.content.Context
import android.graphics.drawable.Drawable
import java.io.IOException

object AssetsReader {

    /**
     * Gets a Drawable from the assets folder.
     * 
     * @param context The context of the application.
     * @param url The path of the drawable in the assets folder.
     * @return Drawable The drawable from the specified path, or null if an error occurs.
     */
    fun getDrawableFromAssets(context: Context, url: String): Drawable? {
        return try {
            // Automatically closes the InputStream after the operation is done
            context.assets.open(url).use { inputStream ->
                // Create a Drawable from the InputStream
                Drawable.createFromStream(inputStream, null)
            }
        } catch (e: IOException) {
            // Log the exception and return null if an error occurs
            e.printStackTrace()
            null
        }
    }
}
攀登最高峰 2024-10-22 12:25:15

在此版本中,您不能,如果您在可绘制文件夹中创建子文件夹,则无法在 xml 文件中使用它,当您使用 android:src 时,它不会被识别。

看一下这个帖子:Android 可绘制目录可以包含子目录吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文