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

发布于 2024-11-02 21:53:09 字数 3086 浏览 1 评论 0原文

我试图通过下面的代码获取存储在 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;
        }
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耳钉梦 2024-11-09 21:53:09

我建议您使用 MediaStore 。另请检查清单文件中的权限

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

老子叫无熙 2024-11-09 21:53:09

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

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

另外,尝试一下

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

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"));
秋叶绚丽 2024-11-09 21:53:09

这可能会有所帮助:

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

//create one Imageview & set its resource mImage

This might be helpful:

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

//create one Imageview & set its resource mImage
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文