Java 从资产文件夹加载图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18973550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:59:49  来源:igfitidea点击:

Load Images from assets folder

javaandroid

提问by Doctor Who

I have an android application in which I have several images in assets folder. Now I want to make an array of that images. Now my problem is :- when our images are in drawable we can make an array like

我有一个 android 应用程序,其中资产文件夹中有几个图像。现在我想制作一组图像。现在我的问题是:- 当我们的图像处于可绘制状态时,我们可以制作一个数组

int x[] = {
    R.drawable.ss,
    R.drawable.aa, R.drawable.sk,
    R.drawable.xx
};

and so on. how can i make an array of images same as above when my images are in assets folder. I want to make an array at class level.

等等。当我的图像位于资产文件夹中时,如何制作与上述相同的图像数组。我想在类级别创建一个数组。

回答by Lokesh

You have to read image by image like below:

您必须逐个图像读取图像,如下所示:

You can use AssetManager to get the InputStream using its open() method and then use decodeStream() method of BitmapFactory to get the Bitmap.

您可以使用 AssetManager 使用其 open() 方法获取 InputStream,然后使用 BitmapFactory 的 decodeStream() 方法获取 Bitmap。

private Bitmap getBitmapFromAsset(String strName)
    {
        AssetManager assetManager = getAssets();
        InputStream istr = null;
        try {
            istr = assetManager.open(strName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Bitmap bitmap = BitmapFactory.decodeStream(istr);
        return bitmap;
    }

回答by Sunil Kumar

If your images are stored in image folder in assets directory then you can get the list of image as way

如果您的图像存储在资产目录中的图像文件夹中,那么您可以通过以下方式获取图像列表

private List<String> getImage(Context context) throws IOException {
      AssetManager assetManager = context.getAssets();
      String[] files = assetManager.list("image");   
      List<String> it = Arrays.asList(files);
      return it; 
}

回答by TommyNecessary

You have wrong meaning about drawables and assests. You can make array od "drawables", because all drawables have own ids in R (like R.dawable.ss), so you can use specified integer to get drawable if you have proper context.

您对可绘制对象和资产的理解有误。您可以创建数组 od“drawables”,因为所有可绘制对象在 R 中都有自己的 id(如 R.dawable.ss),因此如果您有适当的上下文,您可以使用指定的整数来获取可绘制对象。

Other way managing files like images is assests. If you want do manage images by theirs ids you must add this images do drawables. In other way, assests files must be managed like a simple files in dir.

管理图像等文件的其他方式是资产。如果你想通过他们的 id 管理图像,你必须添加这个图像做 drawables。换句话说,资产文件必须像 dir 中的简单文件一样进行管理。

You must get files from assets AssetManager am=this.getAssets();and then prepare file to read/write. If you have images you can do something like this:

您必须从资产中获取文件AssetManager am=this.getAssets();,然后准备文件以进行读/写。如果您有图像,您可以执行以下操作:

try {    
    Bitmap bmp=BitmapFactory.decodeStream(am.open("009.gif"));
    imageView.setImageBitmap(bmp);

} catch (IOException e) {
    e.printStackTrace();
}

回答by sachin10

 // load image from asset folder 
        try {
            // get input stream
            InputStream ims = getAssets().open("avatar.jpg");
            // load image as Drawable
            Drawable d = Drawable.createFromStream(ims, null);
            // set image to ImageView
            mImage.setImageDrawable(d);
        }
        catch(IOException ex) {
            return;
        }

  or you can create drawable array
    Drawable d []={d};