我见过Android的API演示(/ApiDemos/src/com/example/android/apis/view/Gallery1.java),但他们从项目中的res文件夹中获取图像。我想创建一个图片库,这些图片放在文件夹中: /mnt/sdcard/Android/data/com.myapp/files/Pictures /
所有我能想到的是这段代码,我想,它会显示所有图像。
public class ExistingPicGallery extends Activity {
private Cursor cursor;
private int columnIndex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery1);
Gallery g=(Gallery)findViewById(R.id.gallery1);
String[] projection = {MediaStore.Images.ImageColumns._ID,MediaStore.Images.Thumbnails.IMAGE_ID,
MediaStore.Images.Thumbnails.KIND};
// Create the cursor pointing to the SDCard
String selection = MediaStore.Images.Thumbnails.KIND +
"=" + // Select only mini's
MediaStore.Images.Thumbnails.MINI_KIND;
cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
null);
// Get the column index of the image ID
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(ExistingPicGallery.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
public ImageAdapter(Context c) {
context = c;
TypedArray a = obtainStyledAttributes(R.styleable.ExistingPicGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.ExistingPicGallery_android_galleryItemBackground, 0);
a.recycle();
}
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) {
ImageView i = new ImageView(context);
// Move cursor to current position
cursor.moveToPosition(position);
// Get the current value for the requested column
int imageID = cursor.getInt(columnIndex);
// obtain the image URI
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
String url = uri.toString();
// Set the content of the image based on the image URI
int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
Bitmap b = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(),
originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
i.setImageBitmap(b);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(0);
return i;
}
private Context context;
;
}
}
我花了很多时间寻找解决方案,但没有成功..
答案 0 :(得分:3)
我在/ApiDemos/src/com/example/android/apis/view/Gallery1.java中进行了以下更改
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return allFiles.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView,
ViewGroup parent) {
ImageView myImageView = new ImageView(mContext);
if (convertView != null)
myImageView = (ImageView) convertView;
else {
myImageView = new ImageView(mContext);
myImageView.setLayoutParams(new GridView.LayoutParams(60, 60));
myImageView.setAdjustViewBounds(false);
myImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
}
Bitmap bitmapImage = BitmapFactory.decodeFile(folder + "/"
+ allFiles[position]);
BitmapDrawable drawableImage = new BitmapDrawable(bitmapImage);
myImageView.setImageDrawable(drawableImage);
return myImageView;
}
private Context mContext;
File folder = new File(
Environment.getExternalStorageDirectory()
.getPath()+"/files/Pictures/");
String[] allFiles = folder.list();
}
因此,我们在此文件夹中获取了一系列图像名称。在示例中,我们得到了drawables的ID数组。
答案 1 :(得分:0)
以下是我为你设计的代码,它使用Cursor来获取SDCard上的图像......
/**
* Displays images from an SD card.
*/
public class SDCardImagesActivity extends Activity {
/**
* Cursor used to access the results from querying for images on the SD card.
*/
private Cursor cursor;
/*
* Column index for the Thumbnails Image IDs.
*/
private int columnIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sdcard);
// 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);
GridView sdcardImages = (GridView) findViewById(R.id.sdcard);
sdcardImages.setAdapter(new ImageAdapter(this));
// Set up a click listener
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
// Get the data location of the image
String[] projection = {MediaStore.Images.Media.DATA};
cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
// Get image filename
String imagePath = cursor.getString(columnIndex);
// Use this path to do further processing, i.e. full screen display
}
});
}
/**
* Adapter for our image files.
*/
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
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) {
ImageView picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
// 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.setScaleType(ImageView.ScaleType.FIT_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}
答案 2 :(得分:0)