从SD卡中获取多张图像以显示在Coverflow中

时间:2012-09-27 01:40:37

标签: android image external sd-card image-gallery

如何在此封面流程中显示多个图像。我得到空指针异常和其他问题。如何从SD卡加载图像,而不知道SD卡上有多少图像?图像数量不固定。通过使用此代码,我能够显示一个没有问题的图像:

i.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/pic03.png"));

它加载了一张图像,从SD卡指定的图像,并使所有的Coverflow图像成为同一图像,pic03.png,大约相同的一个图像填充了封面流程。这很棒,但我想加载SD卡上的所有图像。因此,每个图像都会填充封面流的每个部分,而不是填充所有部分的相同图像。

完全迷失在这里并尝试这样做并从Logcat得到空指针异常并想知道为什么?有谁知道如何让这个工作?

下面是我正在使用的代码,为您提供一些想法: 请注意,从SD卡上的指定地址加载一个图像时它确实有效。至少那部分工作得很好,但多个图像?

// added this code inside the onCreate method to load the cursor with images only if the IS_PRIVATE column
// in the sqlite database is equal to the integer 1 

String[] img = { MediaStore.Images.Media._ID };
imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, img,
MediaStore.Images.Media.IS_PRIVATE + "='" + 1 +"'",null, MediaStore.Images.Media._ID + "");
image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
count = imagecursor.getCount();

//-------------------------------------------------------------------
// ImageAdapter class is a nested class inside of the CoverFlowExample class

public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private FileInputStream fis;

// originally earlier version of this app loaded images from the R.drawable folder like this

private Integer[] mImageIds = {

  //  R.drawable.pic01,
  //  R.drawable.pic02,
  //  R.drawable.pic03,
  //  R.drawable.pic04,
  //  R.drawable.pic05,
  //  R.drawable.pic06,
  //  R.drawable.pic07,
  //  R.drawable.pic08,
  //  R.drawable.pic09
};

//---------------------------------------------------------------------
// getView() method that is in the ImageAdapter class that extends the BaseAdapter class
// this class is a nested class inside the CoverFlowExample class that extends Activity.
// the CoverFlowExample class is used to implement the coverflow part of the app as an Activity

public View getView(int position, View convertView, ViewGroup parent) {
  // to load from resources like SD card
  ImageView i = new ImageView(mContext);

 // use for single image -->  i.setImageBitmap(BitmapFactory.decodeFile("/mnt/sdcard/pic03.png"));

 image_column_index = imagecursor.getColumnIndexOrThrowMediaStore.Images.Media.DATA);
 imagecursor.moveToPosition(position);

 int id = imagecursor.getInt(image_column_index);
 i.setImageURIUri.withAppendedPathMediaStore         .Images.Media.EXTERNAL_CONTENT_URI, ""+ id));

 // image from R.drawable use -->  i.setImageResource(mImageIds[position]);
 i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
 i.setScaleType(ImageView.ScaleType.MATRIX);            
 return i;

  // return mImages[position];  <-- not using this as im am not getting image from R.drawable
}

1 个答案:

答案 0 :(得分:1)

问题似乎String[] img = { MediaStore.Images.Media._ID } 这是imagecursor中包含的唯一列,而您尝试获取DATA列,imagecursor.getColumnIndexOrThrowMediaStore.Images.Media.DATA)