调用系统Gallery并返回选择的图片Url

private final static int LAUNCH_GALLERY = 3;   //数字自定义  此处用来返回requestCode;

在需要调用系统gallery的地方调用如下代码:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),LAUNCH_GALLERY);


在onActivityResult方法中
Uri _uri = data.getData();

// this will be null if no image was selected...
if (_uri != null) {
// now we get the path to the image file
Cursor cursor = getContentResolver().query(_uri, null,
null, null, null);
cursor.moveToFirst();
String imageFilePath = cursor.getString(1); //返回图片的地址
cursor.close();

这是我的onActivityResult方法:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == LAUNCH_CAMERA && resultCode == Activity.RESULT_OK){
if(data == null){
// String path = ((Uri)data.getExtras().get(MediaStore.EXTRA_OUTPUT)).getPath();
Intent intent = new Intent(this,RecognizeResultActivity.class);
intent.putExtra(IMAGE_PATH, mImagePath);
startActivity(intent);
}
}

if(requestCode == LAUNCH_GALLERY && resultCode == Activity.RESULT_OK){
Uri _uri
= data.getData();

// this will be null if no image was selected...
if (_uri != null) {
// now we get the path to the image file
Cursor cursor = getContentResolver().query(_uri, null,
null, null, null);
cursor.moveToFirst();
String imageFilePath
= cursor.getString(1); //返回图片的地址
cursor.close();

Intent intent
= new Intent(this, RecognizeResultActivity.class);
intent.putExtra(IMAGE_PATH, imageFilePath);
startActivity(intent);
}

}
}

  

你可能感兴趣的:(gallery)