在我的Android相机应用程序中,我使用此代码从Android Gallery获取Select Image。
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
//startActivity(intent);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
//finish();我得到了画廊,一切看起来都很好。但是,当我在画廊上选择永久图像时,什么也没有发生,画廊的活动完成了。
我想选择该图像并对其执行一些操作。这怎么可能呢?
发布于 2011-11-01 14:44:34
在启动ActivityForResult时,您需要编写onActivityResult,您可以像这样获得被调用意图的输出,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);
Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);
iv.setImageBitmap(croppedImage); //set to your imageview
}
}发布于 2011-11-01 14:37:45
在onActivityResult中执行此操作。这里的Uri将是所选图像的Uri。
发布于 2011-11-01 14:45:24
private static final int GALLERY_PICK = 0;
/**
* Start gallery pick activity.
*/
protected void startGalleryPickActivity() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
photoPickerIntent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, GALLERY_PICK);
}
/**
* Gets the temp uri.
*
* @return the temp uri
*/
private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}
/**
* Gets the temp file.
*
* @return the temp file
*/
private File getTempFile() {
if (isSDCARDMounted()) {
imagePath = Environment.getExternalStorageDirectory() + "/temp.jpg";
File f = new File(Environment.getExternalStorageDirectory(),
"temp.jpg");
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
return f;
} else {
return null;
}
}
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
switch (requestCode) {
case GALLERY_PICK:
if (resultCode == RESULT_OK) {
//Write your code here
}
break;
default:
break;
}
}尝试使用此代码。
https://stackoverflow.com/questions/7963163
复制相似问题