我正在尝试显示从相机点击的图片或从网格视图中的图库中选择的图片。从相机点击的图像工作正常,但我无法从图库中获取图像并在gridview中显示。以下是我正在使用的代码:
这里" mCurrentPhotoPath"是最后拍摄的图像的文件路径
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK)
{
// Save Image To Gallery
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE );
File f = new File(mCurrentPhotoPath );
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
// Add Image Path To List
myList.add( mCurrentPhotoPath);
// Refresh Gridview Image Thumbnails
gridview.invalidateViews();
}
else if (requestCode == 2 && resultCode == RESULT_OK)
{
//**New some suggestion here**
Uri selectedImageUri = data.getData();
String s = getRealPathFromURI(selectedImageUri);
myList.add(s);
}
}
}
答案 0 :(得分:0)
如果您尝试提供图片库,请尝试使用
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]); String picturePath = cursor.getString(columnIndex); cursor.close(); ImageView imageView =(ImageView)findViewById(R.id.imgView); imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); } }
gulp
startActivityForResult(i,RESULT_LOAD_IMAGE);