2011-04-25 91 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 
相关问题