您好我正在开发一个Android图库应用,我从内置图库中提取图像并显示它。我使用的代码如下
String[] projection = {MediaStore.Images.Thumbnails._ID};
Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0)
{
Log.e("size 0","0");
}
问题是当我在只有内部存储(Galaxy Nexus)的手机上运行此代码时,即使内置图库中有图像,我也会收到大小为零的Log。我该如何解决这个问题。 请帮助。谢谢!
答案 0 :(得分:3)
在您的按钮上尝试此操作,例如“浏览”
browse.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
并且您还可以将所选图像设置为ImageView
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
picturePath = cursor.getString(columnIndex);
cursor.close();
imageView = (ImageView) findViewById(R.id.property_image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
在第一个代码块中,我将startActivityForResult(i, RESULT_LOAD_IMAGE);
这个返回结果用于被调用的活动,我们可以通过第二个代码块获得此结果,并在ImageView中设置所选图像
答案 1 :(得分:2)
要获取图库图片列表,您可以尝试使用
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media.DATE_TAKEN
};
Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
Cursor cur = getContentResolver().query(imageUri,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
null // Ordering
);
Log.i("Images Count"," count="+cur.getCount());
答案 2 :(得分:2)
String[] projection = {MediaStore.Images.Media._ID};
将MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI
替换为MediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null,
null);
int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
int size = cursor.getCount();
// If size is 0, there are no images on the SD Card.
if (size == 0)
{
Log.e("size 0","0");
}
编辑:获取缩略图列表并从光标获取图像URI
Uri uri=MediaStore.Images.Thumbnails.getContentUri("external");
Cursor cursor=MediaStore.Images.Thumbnails.queryMiniThumbnails
(getContentResolver(), uri, MediaStore.Images.Thumbnails.MINI_KIND,null);
if( cursor != null && cursor.getCount() > 0 ) {
String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
}