嗨我无法在下面的代码之后调用onActivityResult方法。
private void ImageChooseOptionDialog() {
Log.i(TAG, "Inside ImageChooseOptionDialog");
String[] photooptionarray = new String[] { "Take a photo",
"Choose Existing Photo" };
Builder alertDialog = new AlertDialog.Builder(TabSample.tabcontext)
.setItems(photooptionarray,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
if (which == 0) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Camera");
_path = Environment
.getExternalStorageDirectory()
+ File.separator
+ "TakenFromCamera.png";
Log.d(TAG, "----- path ----- " + _path);
media = _path;
// File file = new File(_path);
// Uri outputFileUri = Uri.fromFile(file);
// Intent intent = new Intent(
// android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT,
// outputFileUri);
// startActivityForResult(intent, 1212);
} else if (which == 1) {
Log.i(TAG, "Which" + which);
Log.i(TAG,
"Inside ImageChooseOptionDialog Gallary");
// Intent intent = new Intent();
// intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
// startActivityForResult(Intent
// .createChooser(intent,
// "Select Picture"), 1);
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
// intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent
.createChooser(intent,
"Select Picture"), 1);
Log.i(TAG, "end" + which);
}
}
});
alertDialog.setNeutralButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.create().show();
}
这是我的onActivityResult方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Inside Onactivity Result");
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Log.i(TAG, "Inside if else Onactivity Result");
// currImageURI is the global variable I’m using to hold the
// content:// URI of the image
Uri currImageURI = data.getData();
String path = getRealPathFromURI(currImageURI);
Log.i(TAG, "Inside Onactivity Result Image path" + path);
}
}
}
请让我知道我在哪里做错了。我正在调用onActivityResult方法,在出现对话框之后= 1。 但是没有在logcat中获取onActivityResult方法中的任何日志。
答案 0 :(得分:1)
这是因为,您可能没有获得RESULT_OK。始终首先检查请求代码,然后检查结果代码。如果要从库中检索图像,请尝试使用onActivityResult()中的代码:
if (requestCode == TAKE_PICTURE_GALLERY) {
if (resultCode == RESULT_OK) {
final Uri selectedImage = data.getData();
final String[] filePathColumn = { MediaStore.Images.Media.DATA };
final Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
final int columnIndex = cursor
.getColumnIndex(filePathColumn[0]);
final String filePath = cursor.getString(columnIndex);
cursor.close();
}
}
并在任何地方使用filePath。 我希望这能解决你的问题。谢谢:))
<强>更新强> 使用此代码开始您的图库活动:
imagePathURI = Uri.fromFile(new File(<your image path>));
final Intent intent = new Intent(Intent.ACTION_PICK, imagePathURI);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imagePathURI);
startActivityForResult(intent, TAKE_PICTURE_GALLERY);
当您想要从图库中检索图像时,MediaStore.EXTRA_OUTPUT指的是Intent-extra的名称,用于指示用于存储所请求图像或视频的内容解析器Uri。所以在这里,你必须通过你将收到你的图像的图像路径。
imagePathURI = Uri.fromFile(new File(<your image path>));
//这里将从您的图片路径创建一个文件。当您收到图像时,您可以从此图像路径访问图像。