【发布时间】:2014-07-31 08:33:28
【问题描述】:
我使用了这段代码,但是:图像没有显示在我的子活动中。请在程序中解释我该怎么做。
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Bundle fieldresults = this.getIntent().getExtras();
String backgroundpath = fieldresults.getString("maps");
// getResources().getAssets().openFd(fileName)
// Drawable image = getResources().getAssets();
// imageView.setImageDrawable(image);
Bitmap background = null;
try {
background = getBitmap(backgroundpath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageView gallerypic = (ImageView) findViewById(R.id.imageView2);
gallerypic.setImageBitmap(background);
private Bitmap getBitmap(String name) throws IOException
{
AssetManager asset = getAssets();
InputStream is = asset.open(name);
Bitmap bitmap = BitmapFactory.decodeStream(is);
return bitmap;
}
【问题讨论】:
-
变量backgroundpath的内容是什么?
-
获取assets文件夹中图片文件的路径
-
我知道,但我认为它不应该是路径,而只是图像名称。顺便说一句,为什么不使用资源 ID(因为它是一种资产)并使用
getResources().getIdentifier(drawableName, "drawable", getPackageName());加载它