【发布时间】:2018-01-20 22:33:11
【问题描述】:
我正在创建一个隐式的 android 意图。手机的相机应用程序打开。但是,当我拍照时,相机应用程序关闭,但启动相机意图的 Activity 未打开。手机进入主屏幕。如果我打开应用程序备份它仍然在相机应用程序中。我可以点击相机上的后退按钮并返回 Activity。
意图从这一行开始。
startActivityForResult(takePhotoIntent, IMAGE_REQUEST_CODE);
这是我正在创建的意图。
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
然后我将 MediaStore.EXTRA_OUTPUT 添加到意图
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePhotoIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
// Error occurred while creating the File
//TODO
Log.e(TAG, e.toString());
return null;
}
// Continue only if the File was successfully created
if (photoFile != null && photoFile.exists()) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.markd.android.fileprovider",
photoFile);
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
} else {
Log.e(TAG, "photoFile not configured");
}
} else {
Log.e(TAG, "ResolveActivity is null");
}
}
这里是 createImageFile 方法。
private File createImageFile() throws IOException {
File image = File.createTempFile(
"home_image_" + UUID.randomUUID().toString(), /* prefix */
".jpg", /* suffix */
getExternalFilesDir(Environment.DIRECTORY_PICTURES) /* directory */
);
if(image.getParentFile().mkdirs()) {
Log.e(TAG, "mkdirs:true");
} else {
Log.e(TAG, "mkdirs:false");
}
if(image.exists()) {
Log.e(TAG, "Image exists");
Log.e(TAG, "Path:"+image.getAbsolutePath());
} else {
Log.e(TAG, "Image does not exist");
}
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
这是返回时应该调用的函数,但没有记录任何内容。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG, "onActivityResult");
if (requestCode == IMAGE_REQUEST_CODE) {
if(resultCode == Activity.RESULT_OK) {
//Process result
} else {
Log.d(TAG, "Result not okay");
}
} else {
Log.e(TAG, "Unknown Request");
}
super.onActivityResult(requestCode, resultCode, data);
}
它正在 Motorola XT1028 Android 5.1 API 22 上进行测试。
【问题讨论】:
-
你覆盖了
onActivityResult?? -
@Xenolion 编辑显示 onActivityResult 的 Override
标签: android android-intent android-activity android-camera-intent