1

如何让多个图像显示在此封面流中。我遇到空指针异常和其他问题。如何能够从 SD 卡加载图像,不知道 SD 卡上有多少图像?图片数量不固定。通过使用以下代码,我能够毫无问题地显示一张图像:

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

它加载了一张图片,从 SD 卡中指定的一张,并将所有的封面流图像制作为同一张图片 pic03.png,大约十几个相同的图片填充了封面流。太好了,但是我想将所有图像加载到 SD 卡上。所以每个图像都填充了coverflow的每个部分,而不是一个相同的图像填充所有它们。

完全迷失在这里并试图这样做并从 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
}
4

1 回答 1

1

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

于 2012-09-27T02:11:16.467 回答