2011-12-04 74 views
1

我使用AssetManager加載圖像表單資源/圖像,加載過程進行得很順利,因爲我可以看到所有圖像已加載到圖庫中,但圖庫只顯示圖框,但是不是圖片,我嘗試了不同的MIME類型PNG,JPG和BMP,並沒有解決。當從資產文件夾加載圖像時,圖庫顯示爲空

這是我加載從資產/圖像圖像的方法

private List<String> getImage() throws IOException 
    { 
    AssetManager assetManager = getAssets(); 
    String[] files = assetManager.list("image"); 
    List<String> it=Arrays.asList(files); 
    return it; 

    } 

這是我imageadapter

public class ImageAdapter extends BaseAdapter 
     { 
     /*聲明變量*/ 
     private Context mContext; 
     private List<String> lis; 

     /*ImageAdapter的構造符*/ 
     public ImageAdapter(Context c,List<String> li) 
     { 
      mContext = c; 
      lis=li; 

     } 

     /*幾定要重寫的方法getCount,傳回圖片數目*/ 
     public int getCount() 
     { 
      return lis.size(); 
     } 

     /*一定要重寫的方法getItem,傳回position*/ 
     public Object getItem(int position) 
     { 
      return position; 
     } 

     /*一定要重寫的方法getItemId,傳並position*/ 
     public long getItemId(int position) 
     { 
      return position; 
     } 

     /*幾定要重寫的方法getView,傳並幾View對象*/ 
     public View getView(int position, View convertView, 
           ViewGroup parent) 
     { 
      /*產生ImageView對象*/ 
      ImageView i = new ImageView(mContext); 
      /*設定圖片給imageView對象*/ 
      Bitmap bm = BitmapFactory.decodeFile(lis. 
           get(position).toString()); 
      i.setImageBitmap(bm); 
      /*重新設定圖片的寬高*/ 
      i.setScaleType(ImageView.ScaleType.FIT_XY); 
      /*重新設定Layout的寬高*/ 
      i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

      return i; 
     }  
     } 
    } 

,這是我發起的畫廊

Gallery g = (Gallery) findViewById(R.id.mygallery); 

      /*新增幾ImageAdapter並設定給Gallery對象*/ 
      try { 
       g.setAdapter(new ImageAdapter(this,getImage())); 
      } catch (IOException e) { 
       Log.e("tag", "取得圖片失敗"); 
      } 

的logcat的打印沒有錯誤

回答

1

只要確保'lis'包含完整的路徑名,否則您可能必須預先指定文件名的路徑。做完試圖進行以下更改

public View getView(int position, View convertView, ViewGroup parent) 
    { 
     if (convertView == null) { 
      ImageView i = new ImageView(mContext); 
     } 
     else { 
      ImageView i = (ImageView) convertView; 
     } 

     i.setScaleType(ImageView.ScaleType.FIT_XY); 
     i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     Bitmap bm = BitmapFactory.decodeFile(lis. 
          get(position).toString()); 
     i.setImageBitmap(bm); 
     return i; 
    }  
+0

我想你的方式,並得到NullPointerException異常,THX反正 – oratis

+0

該視圖不是空的,圖像在那裏,我可以看到框架或點擊它,但圖片只是變黑 – oratis

1

嘗試這樣做......

private List<String> getImage() throws IOException { 
     AssetManager assetManager = getAssets(); 
     String[] files = assetManager.list("image"); 
     List<String> list = new ArrayList<String>(); 
     for (String it : files) { 
      list.add("image" + File.separator + it); 
     } 
     return list; 

    } 





public class ImageAdapter extends BaseAdapter { 
     private Context mContext; 
     private List<String> list; 

     public ImageAdapter(Context c, List<String> list) { 
      mContext = c; 
      this.list = list; 
     } 

     @Override 
     public int getCount() { 
      return list.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      ImageView iv = null; 
      try { 

       if (convertView == null) { 
        convertView = new ImageView(mContext); 
        ((ImageView) convertView).setAdjustViewBounds(true); 
        convertView.setLayoutParams(new Gallery.LayoutParams(
          LayoutParams.WRAP_CONTENT, 
          LayoutParams.WRAP_CONTENT)); 
        convertView.setBackgroundResource(R.drawable.picture_frame); 
       } 
       iv = (ImageView) convertView; 

       InputStream is = getAssets().open(list.get(position)); 

       Bitmap bm = BitmapFactory.decodeStream(is); 

       iv.setImageBitmap(bm); 
      } catch (Throwable ex) { 
      } 
      return iv; 
     } 
    } 

現在您的onCreate方法

private Gallery galery; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     galery = (Gallery) findViewById(R.id.gallery); 
     try { 
      galery.setAdapter(new ImageAdapter(this, getImage())); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
相關問題