ACTION_IMAGE_CAPTURE onActivityResult 的意图为空,数据为空
ACTION_IMAGE_CAPTURE onActivityResult has empty intent with empty data
阅读此文档后https://developer.android.com/training/camera/photobasics我想拍照并将照片的 uri 存储到给定变量中。问题是我使用 takePictureIntent.putExtra(Constants.INTENT_EXTRA_VARNAME, variable)
和 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
将变量名和 uri 存储到意图中,但在 onActivityResult 中,意图数据为空,并且存储的变量和 uri 都不为空。我需要知道 uri 和变量。我究竟做错了什么?
我需要将信息作为意图数据传递,因为触发显示相机动作的 class 与 activity class.
不同
class 动作:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(Constants.INTENT_EXTRA_VARNAME, variable);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(SectionManager.getInstance().getCurrentActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(SectionManager.getInstance().getCurrentActivity(), SectionManager.getInstance().getCurrentActivity().getPackageName(), photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
SectionManager.getInstance().getCurrentActivity().startActivityForResult(takePictureIntent, Constants.REQUEST_IMAGE_CAPTURE);
}
}
my activity 收到结果:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if( requestCode == Constants.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
String varName;
String path;
if(intent != null) {
varName = intent.getStringExtra(Constants.INTENT_EXTRA_VARNAME);
path = Util.getRealPathFromURI(SectionManager.getInstance().getCurrentActivity(), intent.getData());
Log.d(DEBUG_TAG, "onActivityResult-> varName: "+varName+" path: "+path);
if (varName != null && path != null) {
VariableManager.put(varName, path);
}
}
}
}
清单、许可 write_external_storage 和此代码:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="/" />
</paths>
您已经有 photoURI
,您用 MediaStore.EXTRA_OUTPUT
发送了它,因此您可以简单地使用它。将 photoURI
保存为全局并直接使用它。查看是否发出 RESULT_OK
则表示图片已被单击并将保存在 EXTRA_OUTPUT
位置。
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if( requestCode == Constants.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
// USe photoURi here
}
}
您不能指望某些 Constants.INTENT_EXTRA_VARNAME
的自定义键将 return 与系统相机 Intent。我对这种事情一无所知。
如果您指定 MediaStore.EXTRA_OUTPUT
,拍摄的图像将写入该路径,并且不会向 onActivityResult
提供任何数据,因此您需要保留文件路径。
PS :您可以全局保存它,并确保在 onSaveInstanceState()
期间保留它。
阅读此文档后https://developer.android.com/training/camera/photobasics我想拍照并将照片的 uri 存储到给定变量中。问题是我使用 takePictureIntent.putExtra(Constants.INTENT_EXTRA_VARNAME, variable)
和 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
将变量名和 uri 存储到意图中,但在 onActivityResult 中,意图数据为空,并且存储的变量和 uri 都不为空。我需要知道 uri 和变量。我究竟做错了什么?
我需要将信息作为意图数据传递,因为触发显示相机动作的 class 与 activity class.
不同class 动作:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(Constants.INTENT_EXTRA_VARNAME, variable);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(SectionManager.getInstance().getCurrentActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(SectionManager.getInstance().getCurrentActivity(), SectionManager.getInstance().getCurrentActivity().getPackageName(), photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
SectionManager.getInstance().getCurrentActivity().startActivityForResult(takePictureIntent, Constants.REQUEST_IMAGE_CAPTURE);
}
}
my activity 收到结果:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if( requestCode == Constants.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
String varName;
String path;
if(intent != null) {
varName = intent.getStringExtra(Constants.INTENT_EXTRA_VARNAME);
path = Util.getRealPathFromURI(SectionManager.getInstance().getCurrentActivity(), intent.getData());
Log.d(DEBUG_TAG, "onActivityResult-> varName: "+varName+" path: "+path);
if (varName != null && path != null) {
VariableManager.put(varName, path);
}
}
}
}
清单、许可 write_external_storage 和此代码:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
file_paths.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="/" />
</paths>
您已经有 photoURI
,您用 MediaStore.EXTRA_OUTPUT
发送了它,因此您可以简单地使用它。将 photoURI
保存为全局并直接使用它。查看是否发出 RESULT_OK
则表示图片已被单击并将保存在 EXTRA_OUTPUT
位置。
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if( requestCode == Constants.REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
// USe photoURi here
}
}
您不能指望某些 Constants.INTENT_EXTRA_VARNAME
的自定义键将 return 与系统相机 Intent。我对这种事情一无所知。
如果您指定 MediaStore.EXTRA_OUTPUT
,拍摄的图像将写入该路径,并且不会向 onActivityResult
提供任何数据,因此您需要保留文件路径。
PS :您可以全局保存它,并确保在 onSaveInstanceState()
期间保留它。