2011-04-25 79 views
0

我試圖通過下面的代碼獲取存儲在SD卡中的所有圖像以顯示在圖庫視圖中。但是當我使用ImageFilter時什麼也沒有顯示,只是一個沒有任何錯誤的空白屏幕。任何建議?嘗試從SD卡中獲取所有圖像以顯示在圖庫視圖

public class Galmix extends Activity { 
    /** Called when the activity is first created. */ 

    private Gallery g; 
    private ImageView imv; 

    private Uri[] mUrls; 
    String[] mFiles=null; 

    class ImageFilter implements FilenameFilter 
    { 
     public boolean accept(File dir, String name) 
     { 
      return (name.endsWith(".JPG")); 
     } 
    } 

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

     File images = new File("/sdcard/"); 
     File[] imagelist = images.listFiles(new ImageFilter()); 
//  File[] imagelist = images.listFiles(); 
//  File[] imagelist = images.listFiles(new FilenameFilter(){ 
//   @Override 
//   public boolean accept(File dir, String name){ 
//    return ((name.endsWith(".JPG"))); 
//   } 
//  }); 

//  File images = new File(Environment.getExternalStorageDirectory().toString()); 
//  images.mkdirs(); 
//  File[] imagelist = images.listFiles(); 

     mFiles = new String[imagelist.length]; 

     for(int i= 0 ; i< imagelist.length; i++){ 
      mFiles[i] = imagelist[i].getAbsolutePath(); 
     } 

     mUrls = new Uri[mFiles.length]; 

     for(int i=0; i < mFiles.length; i++){ 
      mUrls[i] = Uri.parse(mFiles[i]); 
     } 

//  i = (ImageView)findViewById(R.id.ImageView01); 
//  i.setImageResource(mImageIds[0]); 

     g = (Gallery) findViewById(R.id.gallery); 
     g.setAdapter(new ImageAdapter(this)); 
     g.setFadingEdgeLength(40); 
//  g.setOnItemClickListener(new OnItemClickListener() { 
//   public void onItemClick(AdapterView<?> parent, View v, int position, long id) { 
//    imv.setImageURI(mUrls[position]);    
//   } 
//  }); 
    } 

    public class ImageAdapter extends BaseAdapter { 
     int mGalleryItemBackground; 
     private Context mContext; 

     public ImageAdapter(Context c) { 
      mContext = c; 
      TypedArray a = obtainStyledAttributes(R.styleable.HelloGallery); 
      mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground, 0); 
      a.recycle(); 
     } 

     public int getCount() { 
      return mUrls.length; 
     } 

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

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

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

      i.setImageURI(mUrls[position]);    
      i.setLayoutParams(new Gallery.LayoutParams(150, 100)); 
      i.setScaleType(ImageView.ScaleType.FIT_XY); 
      i.setBackgroundResource(mGalleryItemBackground); 

      return i; 
     } 
    } 
} 

回答

0

您是否檢查File images = new File("/sdcard/");是否正在返回實際的SD卡根文件夾?改爲使用File images = Environment.getExternalStorageDirectory();

另外,在mFiles[i] = imagelist[i].getAbsolutePath();之後添加一行Log.d ("Galmix", mFiles[i]);,以便知道是否獲取文件列表。

另外,儘量

return (name.toUpperCase().endsWith(".JPG")); 
+0

沒有發生時,我添加登錄,甚至我改文件中的圖像= Environment.getExternalStorageDirectory()來了; 但是當我沒有使用過濾器像File [] imagelist = images.listFiles();我的應用程序將顯示一個包含圖像的圖庫的空白框。 而另一個是我只關注一個文件夾,如文件圖像=新文件(「/ sdcard/TestPics」);它恰好在圖庫視圖中顯示圖像。 你有什麼建議嗎? 非常感謝。 – 2011-04-25 06:31:25

+0

@Lumluk也許你有混合的圖像與小寫和大寫擴展?嘗試上面的添加。 – Aleadam 2011-04-25 15:48:30

0

這可能會有所幫助:

Drawable mImage; 
// you can use loop over here 
mImage = Drawable.createFromPath("pathName"); 

//create one Imageview & set its resource mImage 
相關問題