【问题标题】:display assets image in listview在列表视图中显示资产图像
【发布时间】: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()); 加载它

标签: android image assets


【解决方案1】:

试试这个方法,希望能帮助你解决问题。

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

【讨论】:

  • @HareshChhelana 它会造成 OutOfMemory 问题吗?我尝试了一些将图像从资产加载到列表视图的方法,但经过一段时间后它会导致 OutOfMemory 问题。
猜你喜欢
  • 1970-01-01
  • 2013-05-18
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
  • 2013-04-24
相关资源
最近更新 更多