Java androide fragmento de tomar una foto

kpokrywja:

En el fragmento que desee tomar una foto, pero tengo un problema, nunca consigo una devolución de llamada a onActivityResult

Mi código :

 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;
    }

onActivityResultNunca se vuelve a llamar. Seguí punto de quiebre en la primera línea del onActivityResultmétodo, pero no se consiga llamar y yo no sé por qué

Rahul Khurana:

está utilizando Uri.fromFile(mCurrentPhotoPath)pero se debe utilizar FileProvider utilizar en su lugar por debajo de código

FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", photoFile)

EDITAR

No se olvide de registrarse para FileProvider en el AndroidManifest.xml

    <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>

dentro de res paquete de crear una subcarpeta llamada XML yprovider_paths.xml

provider_paths.xml contenido

<?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>

EDIT 2

Estás pasando objeto newFile dentro método FileProvider. En su lugar, utilizar la que ya ha creado. Además, se está utilizando getActivity()como contexto se debe fragmentar contexto lugar. vea abajo:

try {
    photoURI = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".provider", photoFile);
} catch (IOException e) {
    e.printStackTrace();
}

Datos 3

Llame a super.onActivityResult()la actividad en el interior

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=337311&siteId=1
Recomendado
Clasificación