与资产文件夹相关的查询

时间:2014-01-31 03:47:14

标签: android assets

考虑我在资产文件夹中有10个图像并将其检索到图像视图。我又将另外10个图像添加到资源文件夹。我能够在图像视图中查看所有20个图像吗?如果不是为什么?如果是这样的话?如何?

这是我用来读取和复制资产文件到SD卡的代码

String[] getImagesFromAssets() {
    AssetManager assetManager = getAssets();
    String[] img_files = null;
    try {
       // img_files = getAssets().list("pictures");
        img_files = assetManager.list("pictures");

    } catch (IOException ex) {
        Logger.getLogger(GameActivity.class
                .getName()).log(Level.SEVERE, null, ex);
    }
    return img_files;


}

void loadImage(String name) {
    AssetManager assetManager = getAssets();

   InputStream in = null;
    OutputStream out = null;
    try {
      in = assetManager.open("pictures/" + name);
      File outFile = new File(getExternalFilesDir(null)+"/"+"pictures/"+ name);

      out = new FileOutputStream(outFile);
      copyFile(in, out);
      in.close();
      in = null;
      out.flush();
      out.close();
      out = null;



      Bitmap myBitmap = BitmapFactory.decodeFile(outFile.getAbsolutePath());

      image.setImageBitmap(myBitmap);



    } catch(IOException e) {
        Log.e("tag", "Failed to copy asset file: " + "pictures/" + name, e);
        return;
    } 

}



private void copyFile(InputStream in, OutputStream out) throws IOException
{
      byte[] buffer = new byte[1024];
      int read;
      while((read = in.read(buffer)) != -1)
      {
            out.write(buffer, 0, read);
      }
}

1 个答案:

答案 0 :(得分:0)

我建议使用第三方库,在android Android-Universal-Image-Loader中使用可接受的URI示例

例如:String imageUri = "assets://image.png";

致谢:Sergey Tarasevich