2011-10-25 135 views
0

我將設置位圖圖像作爲ImageView源的一個小問題。下面是我使用的代碼:Android設置ImageView位圖爲源問題

BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inTempStorage = new byte[16*1024]; 

    String path = Environment.getExternalStorageDirectory()+"/Stampii/"+objectId+".png"; 
    Log.i("","path : "+path); 

    b = BitmapFactory.decodeFile(path,options); 
    if(b==null){ 
     Log.i("","Bitmap is null"); 
    } 

viewFlow = (ViewFlow) findViewById(R.id.viewflow); 
     viewFlow.setAdapter(new ImageAdapter(Cards.this, b),position); 

這裏我如何保存圖像:

File myDir=new File("/sdcard/Stampii"); 
        myDir.mkdirs(); 

        String filename = objectId+".png"; 
        File file = new File(myDir, filename); 
        FileOutputStream fos; 


        fos = new FileOutputStream(file); 
        fos.write(mediaCardBuffer); 
        fos.flush(); 
        fos.close(); 

ImageAdapter:

ublic class ImageAdapter extends BaseAdapter { 

private LayoutInflater mInflater; 

private Bitmap bitmap; 

public ImageAdapter(Context context, Bitmap image) { 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    bitmap = image; 
} 

@Override 
public int getCount() { 
    return 0; 
} 

@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) { 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.image_item, null); 
    } 
    ((ImageView) convertView.findViewById(R.id.imgView)).setImageBitmap(bitmap); 
    return convertView; 
}} 

圖像是SD卡,我可以看到它和名稱和路徑是正確的。它只是沒有出現。任何想法是我的錯誤?

回答

3

我會想象getView永遠不會被調用,因爲你的適配器返回你有0個項目。嘗試返回1的計數或您要顯示的位圖的多個實例。