尝试从SD卡中获取所有图像以在图库视图中显示

时间:2011-04-25 05:05:37

标签: android android-gallery

我正在尝试通过以下代码将存储在SD卡中的所有图像显示在图库视图中。但是当我使用ImageFilter时,没有任何显示,只是一个没有任何错误的空白屏幕。有什么建议吗?

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

3 个答案:

答案 0 :(得分:0)

您是否检查过File images = new File("/sdcard/");是否正在返回实际的SD卡根文件夹?请尝试使用File images = Environment.getExternalStorageDirectory();

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

另外,试试

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

答案 1 :(得分:0)

我建议您使用MediaStore。还要检查你的清单文件中的权限

答案 2 :(得分:0)

这可能会有所帮助:

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

//create one Imageview & set its resource mImage