在listview中显示资产图像

时间:2014-07-31 08:33:28

标签: android image assets

我使用了这段代码,但是:我的孩子活动中没有显示图像。请在程序中解释我该怎么办。

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

1 个答案:

答案 0 :(得分:0)

请尝试这种方式,希望这有助于您解决问题。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        ImageView gallerypic = (ImageView) findViewById(R.id.imageView2);

        String backgroundpath = getIntent().getExtras().getString("maps");
        String backgroundextension = getIntent().getExtras().getString("extension");

        if(getImageFromAssert(backgroundpath,backgroundextension)!=null){
            gallerypic.setImageDrawable(getImageFromAssert(backgroundpath,backgroundextension));
        }
    }

    private Drawable getImageFromAssert(String name,String extension){
        try {
            InputStream ims = getAssets().open(name + "." + extension);
            return Drawable.createFromStream(ims, null);
        }catch (Throwable e){
            e.printStackTrace();
        }
        return null;
    }