如何动态地从SD卡中的文件夹显示图片文件夹、动态、图片、SD

2023-09-03 20:58:16 作者:浅汐

我要创建一个装有从驻留在SD卡的特定文件夹的图像一个gridview。该文件夹的路径是/ SD卡/图像/。我试着用这个code,应用程序  正在采取太多的时间来加载,它是只显示一个图像。

 公共类ImAdapterh扩展了BaseAdapter {

       文件DIR =新的文件(Environment.getExternalStorageDirectory()/ myImages /);

。诠释计数= dir.list()长;

的String []文件名= dir.list();

 私人语境mContext;

    公共ImAdapterh(上下文C){
        mContext = C;
    }

    公众诠释getCount将(){
        返回计数;
    }

    公共对象的getItem(INT位置){
        返回null;
    }

    众长getItemId(INT位置){
        返回0;
    }

    //创建由适配器引用的每个项目的新的ImageView
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ImageView的ImageView的= NULL;


      对于(字符串bitmapFileName:文件名)
      {
           如果(convertView == NULL)
           {//如果它不回收,初始化一些属性
               ImageView的=新ImageView的(mContext);
               imageView.setLayoutParams(新GridView.LayoutParams(85,85));
               imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
               imageView.setPadding(8,8,8,8);

               BMP位= BitmapFactory.de codeFILE(dir.getPath()+/+ bitmapFileName);
               的System.out.println(DIR);
               imageView.setImageBitmap(BMP);
            }其他
            {
            ImageView的=(ImageView的)convertView;

            }
    }
        返回ImageView的;
        }
 

应用程序正在占用太多的时间,它会显示只有一个图像

这是我的活动类

 公共无效的onCreate(包savedInstanceState){
      super.onCreate(savedInstanceState);
      的setContentView(R.layout.main);
       GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
       gridview.setAdapter(新ImAdapterh(本));}
 
购买的手机SD卡在手机文件夹里显示的时间不正确,怎么调出来 以前没有过这样

解决方案   

该文件夹的路径是/ SD卡/图像/".

没有,它不是,至少对于大多数设备。在Android中切勿硬code路径。始终使用 Environment.getExternalStorageDirectory()来获得外部存储设备根目录下。

  

我试着用这个code,该应用程序是占用太多的时间来加载,它是只显示一个图像。

当然。有什么,你在这里做很少是正确的。

目前最大的问题是,你的 getView()负载的每次的图像在同一的ImageView ,通过您的适配器返回每行。 presumably,你应该只加载的一个的图像到的ImageView 对于一个给定的行。

您的下一个最大的问题是,你正在做磁盘I / O的主应用程序线程上。

I have to create a gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is "/sdcard/images/". i tried with this code,the app is taking too much time to load and it is displaying only one image.

    public class ImAdapterh extends BaseAdapter{

       File dir=new File(Environment.getExternalStorageDirectory(),"/myImages/");

int count=dir.list().length; 

String[] fileNames = dir.list();

 private Context mContext;

    public ImAdapterh(Context c) {
        mContext = c;
    }

    public int getCount() {
        return count;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;        


      for(String bitmapFileName : fileNames)
      {
           if (convertView == null) 
           {  // if it's not recycled, initialize some attributes
               imageView = new ImageView(mContext);
               imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
               imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
               imageView.setPadding(8, 8, 8, 8);                   

               Bitmap bmp = BitmapFactory.decodeFile(dir.getPath() + "/" + bitmapFileName);
               System.out.println(dir);
               imageView.setImageBitmap(bmp);  
            }else 
            {
            imageView = (ImageView) convertView;            

            }
    } 
        return imageView;
        }

the app is taking too much time and it is displaying only one image

this is my activity class

      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);        
       GridView gridview = (GridView) findViewById(R.id.gridview);
       gridview.setAdapter(new ImAdapterh(this));}

解决方案

The path to the folder is "/sdcard/images/".

No, it is not, at least not for most devices. Never hard-code paths in Android. Always use Environment.getExternalStorageDirectory() to get the root of external storage.

i tried with this code,the app is taking too much time to load and it is displaying only one image.

Of course. There is very little correct in what you have done here.

The biggest problem is that your getView() loads every image into the same ImageView, for each and every row returned by your Adapter. Presumably, you should be only loading one image into the ImageView for a given row.

Your next-biggest problem is that you are doing disk I/O on the main application thread.

 
精彩推荐
图片推荐