我正在开发android应用程序。在我的应用程序中,我想在库视图中显示sd卡文件夹中的图像。我正在使用BaseAdapter。我试图在我的项目的资源文件夹中显示drawable文件夹中的图像,它工作正常。 我尝试了以下代码:
public class ImageAdapter extends BaseAdapter {
private Context context;
public static Integer[] imageIDs={
R.drawable.sp1,R.drawable.sp2,
R.drawable.sp3,R.drawable.sp4,
R.drawable.sp5,R.drawable.sp6,
R.drawable.sp7,R.drawable.sp8,
R.drawable.sp9
};
public ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageIDs.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View
convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView image=new ImageView(context);
image.setImageResource(imageIDs[position]);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(120,120));
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
return image;
}
现在我想显示sd卡文件夹中的图像并以图库格式显示它们。 为此,我试图获取阵列中的所有图像,但我不知道如何以数组格式获取它们。我可以通过以下方式访问sd文件夹中的图像。
File path = new File(Environment.getExternalStorageDirectory()+"/download/sp1.jpg");
如何以数组格式获取它们并在库视图中显示它们。或者是否有其他替代方式来显示它们而不是数组?
需要帮助....谢谢......
答案 0 :(得分:1)
您需要将图像加载为位图:
getView(...){
File f = new File(directory, filename);
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
...
ImageView image=new ImageView(context);
image.setImageBitmap(bitmap);
}
答案 1 :(得分:1)
见下面的代码
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
// It have to be matched with the directory in SDCard
boolean exist = new File(Environment.getExternalStorageDirectory()
+ File.separator + "download").exists();
if (exist) {
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "download");
File[] files = f.listFiles();
File file;
for (int i = 0; i < files.length; i++) {
file = files[i];
Bitmap bitmap = decodeFile(file);
return bitmap;
}
}
}
你能这样做吗
@Override
public int getCount() {
// TODO Auto-generated method stub
return files.length;
}
@Override
public View getView(int position, View
convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView image=new ImageView(context);
//image.setImageResource(imageIDs[position]);
File file = files[position]
Bitmap bitmap = decodeFile(file);
image.setImageBitmap(bitmap);
image.setAdjustViewBounds(true);
image.setLayoutParams(new Gallery.LayoutParams(120,120));
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
return image;
}