我必须创建一个grIDvIEw,其中装有来自SDCard上特定文件夹的图像.文件夹的路径是“ / sdcard / images /”.我尝试使用此代码,该应用
正在加载太多时间,并且仅显示一张图像.
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; }
该应用程序花费了太多时间,并且仅显示一张图像
这是我的活动课
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/”.
不,不是,至少对于大多数设备而言并非如此.请勿在AndroID中对路径进行硬编码.始终使用Environment.getExternalStorageDirectory()获取外部存储的根.
i trIEd with this code,the app is taking too much time to load and it is displaying only one image.
当然.您在这里所做的工作很少正确.
最大的问题是,对于您的适配器返回的每一行,getVIEw()会将每个图像加载到同一ImageVIEw中.大概,对于给定的行,您应该只将一个图像加载到ImageVIEw中.
下一个最大的问题是您正在主应用程序线程上执行磁盘I / O.
总结以上是内存溢出为你收集整理的android-如何动态显示SD卡中文件夹中的图像全部内容,希望文章能够帮你解决android-如何动态显示SD卡中文件夹中的图像所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)