java - Java android片段拍照
问题描述
在片段中我想拍照但我有问题,我从来没有收到回电onActivityResult
我的代码:
private void dispatchTakePictureIntent() {
int width = 960;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Log.e("error",ex.getMessage());
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = null;
try {
photoURI = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", createImageFile());
} catch (IOException e) {
e.printStackTrace();
}
// cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
} else if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ignored) {
}
if (photoFile != null) {
mCurrentPhotoPath = photoFile;
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoPath));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private File createImageFile() throws IOException {
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String imageFileName = "smok" + timeStamp;
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
imagesDir /* directory */
);
mCurrentPhotoPath = new File(image.getAbsolutePath());
return image;
}
onActivityResult
永远不会被调用。我在方法的第一行保留断点,onActivityResult
但它没有被调用,我不知道为什么
解决方案
您正在使用Uri.fromFile(mCurrentPhotoPath)
,但您应该使用FileProvider而不是使用下面的代码
FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", photoFile)
编辑
不要忘记在AndroidManifest.xml中注册 FileProvider
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
在res包中创建一个名为xml的子文件夹和provider_paths.xml
provider_paths.xml
内容
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--<external-path name="external_files" path="."/>-->
<external-path name="external_files" path="/"/>
</paths>
编辑 2
您在 FileProvider 方法中传递 newFile 对象。相反,请使用您已经创建的那个。此外,您使用getActivity()
的上下文应该改为分段上下文。见下文:
try {
photoURI = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", photoFile);
} catch (IOException e) {
e.printStackTrace();
}
编辑 3
调用super.onActivityResult()
内部活动
推荐阅读
- javascript - 为什么我的 vue 代码没有按预期工作?
- android - 由于 NullPointerException,无法启动活动 ComponentInfo
- c++ - C ++静态继承中的取消引用指针问题?
- javascript - 使用声明模块打字稿向类公开属性
- java - 普通实例化和包括方法引用的实例化之间的区别
- node.js - 在 Express 应用程序中为 iOS 应用程序设置回调 URL
- postgresql - Odoo 表分区
- r - 基于值出现的顺序计数
- c# - 如何在不使用复制的情况下将本地 SQL Server 数据库中的数据同步到远程 SQL Server?
- php - 创建一个只包含一个插件的新角色