在我的应用程序中,我需要为用户实现一个选项,以便从SD卡或手机内存中选择图像。我使用自定义封面流来动画图像。我编写了一个代码,使用光标从SD卡中检索图像。但我只得到了一些图像,而不是全部。这些图像在我的封面流程中多次显示。每张图片都显示两次。
如果SD卡上没有图像,则应用程序崩溃。
这是我的代码,请有人帮忙
public class CoverFlowActivityMain extends Activity {
private Cursor cursor;
private int columnIndex;
private File file;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CoverFlow coverFlow;
coverFlow = new CoverFlow(this);
file = new File("/sdcard/");
// I am using this file to check iamges on sd-card,
// but this does not search files in subdirectories.
File[] allFiles = file.listFiles();
for(int i=0; i<allFiles.length; i++) {
Log.v("File: "+i, ""+allFiles[i].getName().toString());
}
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Thumbnails._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
MediaStore.Images.Thumbnails.IMAGE_ID);
// Get the column index of the Thumbnails Image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
coverFlow.setAdapter(new ImageAdapter(this));
ImageAdapter coverImageAdapter = new ImageAdapter(this);
coverFlow.setAdapter(coverImageAdapter);
coverFlow.setSpacing(-5);
coverFlow.setSelection(0, true);
coverFlow.setAnimationDuration(1500);
setContentView(coverFlow);
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
//-- Use this code if you want to load from sdcard --\\
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(mContext);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// Set the content of the image based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setLayoutParams(new CoverFlow.LayoutParams(380, 450));
picturesView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//Make sure we set anti-aliasing otherwise we get jaggies
BitmapDrawable drawable = (BitmapDrawable) picturesView.getDrawable();
drawable.setAntiAlias(true);
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}