android - 拍照时捆绑键返回 null
问题描述
我正在使用 ro 启动从相机捕获图像的意图:
public void takePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
Bundle bundle = new Bundle();
f = createImageFile(); // creates a new image file into which I want to store the photo
bundle.putString( "currentPhotoPath", f.getAbsolutePath() );
intent.putExtras(bundle);
startActivityForResult(intent, CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE);
}
catch (IOException e) {
e.printStackTrace();
}
}
crates 一个空的f.createImageFile
Image 对象,我想在其中存储从相机拍摄的照片。我试图将这个图像对象的路径传递给捆绑包,如下所示:
bundle.putString( "currentPhotoPath", f.getAbsolutePath() );
然后我使用这段代码得到结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAPTURE_IMAGE_FULLSIZE_ACTIVITY_REQUEST_CODE ) {
Bundle bundle = data.getExtras();
String currentPhotoPath = bundle.getString("currentPhotoPath");
File takenPhoto = new File("currentPhotoPath");
if (resultCode == RESULT_OK) {
if(takenPhoto.length() <= 0) {
takenPhoto.delete();
}
this.checkForUploads();
}
else if(resultCode == RESULT_CANCELED) {
takenPhoto.delete();
}
}
}
我期待在 data.getExtras()
保存新图像对象路径的键“currentPhotoPath”中找到,但它始终为空。相反,我得到的唯一关键是我认为它包含实际位图数据的数据。我在 so 上看到过类似的问题,我认为我的做法是正确的。
解决方案
我在 so 上看到过类似的问题,我认为我的做法是正确的。
对不起,不行。
您投入的额外Intent
内容Intent
。在您的情况下,它会进行一些具有ACTION_IMAGE_CAPTURE
<intent-filter>
. 该代码如何处理额外内容取决于该活动的开发人员。在您的情况下,由于您自己发明了一个新Intent
的附加功能,因此您的附加功能将被忽略,因为ACTION_IMAGE_CAPTURE
活动的开发人员不会知道要寻找它。
Intent
交付对象与您使用的onActivityResult()
对象完全不同。特别是,您添加的额外内容不需要包含在其他应用程序的响应中。Intent
startActivityForResult()
startActivityForResult()
Intent
Intent
推荐阅读
- javascript - 通过 UID 从 firebase 获取用户
- rust - 如何反序列化带有可选内部标签的枚举?
- internet-explorer - IE 和 Edge 上的网页下拉菜单
- html - 如果一行中的列不适合屏幕,则使该列显示垂直滚动条
- python - 如何对复杂的 Python 列表进行排序?
- xamarin - 向 Pin Xamarin.forms 添加问题和答案
- c# - 没有依赖关系的类的单元测试
- python - 如何拆分字符串列表并使用递归返回元组?
- reactjs - 如何从 React Bootstrap 中的下拉导航项中删除插入符号
- git - 如何在 Git 中使用多个堆叠分支进行变基?