1

我正在开发 android 应用程序。在我的应用程序中,我想在图库视图中显示 sd 卡文件夹中的图像。我正在使用 BaseAdapter。我试图在我的项目的资源文件夹中显示可绘制文件夹中的图像,它工作正常。我尝试了以下代码:

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");

如何以数组格式获取它们并在图库视图中显示它们。或者有没有其他替代方法来显示它们而不是数组?

需要帮助....谢谢....

4

2 回答 2

1

您需要将图像加载为位图:

getView(...){
File f = new File(directory, filename);
Bitmap bitmap = BitmapFactory.decodeFile(f.getPath());
...
ImageView image=new ImageView(context);
image.setImageBitmap(bitmap);
}
于 2012-07-27T04:59:59.810 回答
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;  
}
于 2012-07-27T05:06:40.317 回答