【问题标题】:Why does "asset not found" error occur in my code?为什么我的代码中出现“资产未找到”错误?
【发布时间】:2014-05-10 10:42:09
【问题描述】:

我的资产文件夹中有一个 .png 文件,当我点击屏幕时,我试图让该图像显示在屏幕上。

InputStream open = null;
try{
    open = asset.open("ic_launcher.png");
    Bitmap bitmap = BitmapFactory.decodeStream(open);

    ImageView image = (ImageView)findViewById(R.id.imageView1);
    image.setImageBitmap(bitmap);
}catch(Exception e){
    e.printStackTrace();
}
finally{
    if(open != null){
        try{
            open.close();
        }catch(IOException e){
            e.printStackTrace();
        }       
    }
}

但我不断收到FileNotFoundException,不知道为什么。

【问题讨论】:

  • ic_launcher.png 到底在哪里?
  • 我的项目的assets 文件夹。
  • 是 ic_launcher.png 存储在 assets.put 你的完整代码.asset 的初始化
  • project_name/assets/ic_launcher.png ?

标签: java android


【解决方案1】:

检查下面的代码行并替换它。

InputStream is = getAssets().open("ic_launcher.png");

【讨论】:

    【解决方案2】:

    使用,

     open =getAssets().open("icon.png");
    

    【讨论】:

    • 为什么要有所作为?
    【解决方案3】:

    试试这个,它可能会工作。

     open = asset.open
    ("ic_launcher");
    

    不要提及扩展。

    【讨论】:

    • 这不起作用。但是,我清理了项目并重新启动它。现在我得到一个不同的错误。我收到 NPE 错误..
    • 忘记了可绘制和使用资产?您存储在资产中的图像不会被压缩。最好使用drawable。
    【解决方案4】:

    试试这个-

    try {
    InputStream ims = getAssets().open("ic_launcher.png");
    Drawable d = Drawable.createFromStream(ims, null);
    imgView.setImageDrawable(d);
    }
    catch(IOException ex) {
    return;
    }
    

    还有check this.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-19
      • 1970-01-01
      • 2019-08-08
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2021-10-18
      相关资源
      最近更新 更多