从assets文件夹加载图像

时间:2013-09-24 05:25:03

标签: java android

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

这样的数组
int x[] = {
    R.drawable.ss,
    R.drawable.aa, R.drawable.sk,
    R.drawable.xx
};

等等。当我的图像在资源文件夹中时,如何制作与上面相同的图像数组。 我想在班级制作一个数组。

4 个答案:

答案 0 :(得分:26)

您必须按照以下图像阅读图像:

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

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

答案 1 :(得分:6)

如果您的图像存储在资源目录中的图像文件夹中,那么您可以按照

的方式获取图像列表
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; 
}

答案 2 :(得分:4)

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

答案 3 :(得分:0)

你对drawables和assests有错误的含义。你可以创建数组od“drawables”,因为所有drawable在R中都有自己的id(比如R.dawable.ss),所以如果你有适当的上下文,你可以使用指定的整数来获得drawable。

管理像图像这样的文件的其他方式是有效的。如果你想通过他们的ID管理图像,你必须添加这些图像做drawables。换句话说,必须像在dir中的简单文件一样管理assests文件。

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

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

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