首页 > 解决方案 > 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但它没有被调用,我不知道为什么

标签: javaandroid

解决方案


您正在使用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()内部活动


推荐阅读