Java “为什么?”;“未找到资产”;我的代码中是否出现错误?
我的资产文件夹中有一个.png文件,我试图在点击屏幕时将该图像显示在屏幕上Java “为什么?”;“未找到资产”;我的代码中是否出现错误?,java,android,Java,Android,我的资产文件夹中有一个.png文件,我试图在点击屏幕时将该图像显示在屏幕上 InputStream open = null; try{ open = asset.open("ic_launcher.png"); Bitmap bitmap = BitmapFactory.decodeStream(open); ImageView image = (ImageView)findViewById(R.id.imageView1); image.setImageBit
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
,不知道为什么。检查代码行下面的内容并替换它
InputStream is = getAssets().open("ic_launcher.png");
使用
open =getAssets().open("icon.png");
试试这个,可能有用
open = asset.open
("ic_launcher");
不要提及扩展。试试这个-
try {
InputStream ims = getAssets().open("ic_launcher.png");
Drawable d = Drawable.createFromStream(ims, null);
imgView.setImageDrawable(d);
}
catch(IOException ex) {
return;
}
另外ic_launcher.png的确切位置在哪里?我的项目的
assets
文件夹。ic_launcher.png是否存储在assets.put您的完整代码。资产的初始化项目名称
/资产
/ic_launcher.png
?为什么会有什么不同?这不起作用。但是,我清理了项目并重新启动了它。现在我得到一个不同的错误。我得到一个NPE错误..忘记了可提取和使用资产?存储在资产中的图像不会被压缩。最好使用可拉伸的。