尝试从SD卡获取所有图像图库视图来显示 [英] Try to get all image from sdcard to show in gallery view

查看:260
本文介绍了尝试从SD卡获取所有图像图库视图来显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让所有存储在SD卡中的由下code画廊视图来显示图像的。但是当我使用的ImageFilter什么也不显示,只是没有任何错误空白屏幕。任何建议?

 公共类Galmix延伸活动{
    / **当第一次创建活动调用。 * /    私人画廊克;
    私人ImageView的IMV;    私人乌里[] mUrls;
    的String [] mFiles = NULL;    类ImageFilter中实现了FilenameFilter
    {
        公共布尔接受(文件目录,字符串名称)
        {
            返回(name.endsWith(JPG));
        }
    }    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        文件中的图像=新的文件(/ SD卡/);
        文件[] =的ImageList images.listFiles(新的ImageFilter());
//文件[] =的ImageList images.listFiles();
//文件[] =的ImageList images.listFiles(新的FilenameFilter(){
// @覆盖
//公共布尔接受(文件目录,字符串名称){
//回报((name.endsWith(JPG)));
//}
//});//文件图像=新的文件(Environment.getExternalStorageDirectory()的toString());
// images.mkdirs();
//文件[] =的ImageList images.listFiles();        mFiles =新的String [imagelist.length]        的for(int i = 0; I< imagelist.length;我++){
            mFiles [I] =图像列表[I] .getAbsolutePath();
        }        mUrls =新的URI [mFiles.length]        的for(int i = 0; I< mFiles.length;我++){
            mUrls [I] = Uri.parse(mFiles [I]);
        }// I =(ImageView的)findViewById(R.id.ImageView01);
// i.setImageResource(mImageIds [0]);        G =(图库论坛)findViewById(R.id.gallery);
        g.setAdapter(新ImageAdapter(本));
        g.setFadingEdgeLength(40);
// g.setOnItemClickListener(新OnItemClickListener(){
//公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
// imv.setImageURI(mUrls [位置]);
//}
//});
    }    公共类ImageAdapter延伸BaseAdapter {
        INT mGalleryItemBackground;
        私人语境mContext;        公共ImageAdapter(上下文C){
            mContext = C;
            TypedArray A = obtainStyledAttributes(R.styleable.HelloGallery);
            mGalleryItemBackground = a.getResourceId(R.styleable.HelloGallery_android_galleryItemBackground,0);
            a.recycle();
        }        公众诠释的getCount(){
            返回mUrls.length;
        }        公共对象的getItem(INT位置){
            返回的位置;
        }        众长getItemId(INT位置){
            返回的位置;
        }        公共查看getView(INT位置,查看convertView,父母的ViewGroup){
            ImageView的I =新ImageView的(mContext);            i.setImageURI(mUrls [位置]);
            i.setLayoutParams(新Gallery.LayoutParams(150,100));
            i.setScaleType(ImageView.ScaleType.FIT_XY);
            i.setBackgroundResource(mGalleryItemBackground);            返回我;
        }
    }
}


解决方案

这可能是有益的:

 可绘制mImage;
//你可以使用循环在这里
mImage = Drawable.createFromPath(路径名);//创建一个ImageView的&安培;设置其资源mImage

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

解决方案

This might be helpful:

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

//create one Imageview & set its resource mImage

这篇关于尝试从SD卡获取所有图像图库视图来显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆