【发布时间】:2012-04-25 14:02:41
【问题描述】:
大家好, 我搜索了很多我的问题,我发现了很多类似问题的帖子,但没有人给我一个正确的解决方案。 我想要的是一个显示 sdcard 文件夹图像的网格视图。我还必须提供拍照的可能性,当返回网格视图时,用新图片更新它。
要拍照,我使用以下代码:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageFileUri());
startActivityForResult(intent, TAKE_PICTURE_ACTIVITY);
getImageFileUri() 是一个函数,它给我一个带有时间戳的图片名称,使用 Environment.getExternalStorageDirectory() 获取 sdcard 路径,并检查文件夹是否存在(如果不存在则创建它)。
目前,我使用光标来获取图像:
private void displayGallery() {
// Query params :
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media._ID};
String selection = MediaStore.Images.Media.DATA + " like ? ";
String[] selectionArgs = {"%Otiama%"};
// Submit the query :
mCursor = managedQuery(uri, projection, selection, selectionArgs, null);
if (mCursor != null) {
mGridView.setOnItemClickListener(this);
mGridView.setAdapter(new ImageAdapter(this, mCursor));
}
else showToast("Gallery is empty : " + uri.toString());
}
这是我的适配器的 getView :
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
// Move cursor to current position
mCursor.moveToPosition(position);
// Get the current value for the requested column
int columnIndex = mCursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int imageID = mCursor.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(mContext.getContentResolver(), originalImageId, MediaStore.Images.Thumbnails.MINI_KIND, null);
imageView.setImageBitmap(b);
return imageView;
}
此代码有效,但速度很慢且不会更新(不会创建新图片的缩略图),即使我在 onActivityResult() 中再次调用 displayGallery() 函数也是如此。好吧,即使我重新加载应用程序,它也不会更新 >
事实上,我想要的是和 ES File Explorer 一样的行为(当你打开一个文件夹时,图片都有一个预览图像,并且它们是异步加载的),我认为它不使用那些** 缩略图。
所以我尝试使用位图工厂将图片加载为位图,但即使有几张图片 (1-2),我也会立即收到“java.lang.OutOfMemoryError:位图大小超出 VM 预算”...我我想我必须调整它们的大小,但如果我这样做了,当我加载 20-30 张图片时,我不会有同样的错误吗?或者问题是每张图片都超出了预算,所以如果我调整它们的大小,我会避免所有这些错误?
抱歉这篇大文章,如果有人可以帮助...
【问题讨论】:
标签: android gridview directory sd-card