Try to get all image from sdcard to show in galler

2019-09-04 07:56发布

I'm trying to get all of the image that stored in sdcard to show in a gallery view by the code below. But when I use ImageFilter nothing shows up, just a blank screen without any error. Any suggestion?

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条回答
Juvenile、少年°
2楼-- · 2019-09-04 08:43

Did you check that File images = new File("/sdcard/"); is returning the actual sdcard root folder? Try using File images = Environment.getExternalStorageDirectory(); instead.

Also, add a line Log.d ("Galmix", mFiles[i]); after mFiles[i] = imagelist[i].getAbsolutePath(); so you know if you are getting the file list or not.

Also, try

return (name.toUpperCase().endsWith(".JPG"));
查看更多
\"骚年 ilove
3楼-- · 2019-09-04 08:48

I would suggest that u make use of MediaStore . Also check for permissions in ur manifest file

查看更多
Explosion°爆炸
4楼-- · 2019-09-04 08:50

This might be helpful:

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

//create one Imageview & set its resource mImage
查看更多
登录 后发表回答