为用相机拍摄的图像创建文件时获取 RuntimeException 和 NullPointerException

Getting RuntimeException and NullPointerException when creating file for image taken with the camera

我正在制作联系人议程应用程序,我需要将用相机拍摄的照片保存到设备图库中。我一直在关注本教程:https://developer.android.com/training/camera/photobasics 并查看并尝试了几个修复程序,但 none 似乎有效。

拍照后报错如下:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.agendaandroid, PID: 4456
    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent {  }} to activity {com.example.agendaandroid/com.example.agendaandroid.NuevaPersonaActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4360)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference
        at com.example.agendaandroid.NuevaPersonaActivity.onActivityResult(NuevaPersonaActivity.java:213)
        at android.app.Activity.dispatchActivityResult(Activity.java:7454)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:4353)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:4402) 
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

这是我的 NuevaPersonaActivity.java:

<pre>
package com.example.agendaandroid;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import clases.Contacto;
import clases.Correo;
import clases.Telefono;
import controlador.BaseDeDatos;

public class NuevaPersonaActivity extends AppCompatActivity {
    Contacto contacto;
    private final int INTENT_GALERIA = 0;
    private final int INTENT_CAMARA = 1;
    private String direccionActualFoto;

    private String[] permisos = {Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nueva_persona);

    }

    public void btnAnadirContacto(View view) {
        //TODO Controlar de manera más efectiva que el input no esté vacío
        //TODO sacar foto con la cámara u obtener foto de galería cuando hagamos click en la imagen.
        //
        //https://developer.android.com/training/camera/photobasics?hl=ES
        ImageView imgFoto = (ImageView) findViewById(R.id.imgFotoEdit);

        String nombre = ((EditText) findViewById(R.id.txtNombreEdit)).getText().toString();
        String notas = ((EditText) findViewById(R.id.txtNotasEdit)).getText().toString();
        String telefono = ((EditText) findViewById(R.id.txtTlfEdit)).getText().toString();
        String correo = ((EditText) findViewById(R.id.txtCorreoEdit)).getText().toString();


        if (nombre.isEmpty() && notas.isEmpty()) {
            Toast.makeText(this, "Información de contacto vacía", Toast.LENGTH_SHORT).show();
        } else {
            try {
                contacto = new Contacto(nombre, notas);
                BaseDeDatos bd = new BaseDeDatos(this);
                bd.anadirContacto(contacto);
                bd.anadirCorreoContacto(new Correo(contacto.getIdentificador(), correo));
                bd.anadirTelefonoContacto(new Telefono(contacto.getIdentificador(), telefono));

                Toast.makeText(this, "Nuevo contacto añadido", Toast.LENGTH_SHORT).show();

                //Refrescamos el listView de la actividad principal.
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);

                finish();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(this, "Ha habido un problema", Toast.LENGTH_SHORT).show();

            }
        }
    }


    public void onClickFoto(View view) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (permisosConcedidos()) {
                dialogoFoto();
            } else {
                pedirVariosPermisos();
            }
        }
    }

    /**
     * Muestra un diálogo para elegir la fuente de la imagen de contacto: cámara o galería.
     * Dentro de cada opción hace la acción correspondiente.
     */
    private void dialogoFoto() {
        AlertDialog.Builder builder = new AlertDialog.Builder(NuevaPersonaActivity.this);
        builder.setTitle("Añadir una foto").setPositiveButton(R.string.camara, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent intentFoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (intentFoto.resolveActivity(getPackageManager()) != null) {
                    File foto = null;
                    try{
                        foto = createImageFile();
                    } catch (IOException e){
                        e.printStackTrace();
                    }
                    if(foto != null){
                        Uri uriFoto = FileProvider.getUriForFile(NuevaPersonaActivity.this, "com.example.agendaandroid.fileprovider", foto);
                        intentFoto.putExtra(MediaStore.EXTRA_OUTPUT, uriFoto);
                        startActivityForResult(intentFoto, INTENT_CAMARA);
                    }

                }
            }
        }).setNegativeButton(R.string.galeria, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                Intent intentGaleria = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intentGaleria, INTENT_GALERIA);
            }
        });

        builder.show();
    }



    /*Este método comprueba si los permisos necesarios han sido aceptados o no por el usuario.
    *Solo funciona si la api es mayor o igual a nuestra mínima (23). Devuelve false con solo un permiso denegado.
    * */
    @RequiresApi( api = Build.VERSION_CODES.M)
    private boolean permisosConcedidos(){
        for (String permiso : permisos){
            if(checkSelfPermission(permiso) != PackageManager.PERMISSION_GRANTED){
                return false;
            }
        }
        return true;
    }

    /*
    Este método crea una lista de los permisos que aún no tenemos concedidos y los vuelve a pedir..
     */
    @RequiresApi( api = Build.VERSION_CODES.M)
    private void pedirVariosPermisos(){
        List<String> permisosNoConcedidos = new ArrayList<>();
        for(String permiso : permisos){
            if(checkSelfPermission(permiso) != PackageManager.PERMISSION_GRANTED){
                permisosNoConcedidos.add(permiso);
            }
        }
        requestPermissions(permisosNoConcedidos.toArray(new String[permisosNoConcedidos.size()]),101);

    }


    @RequiresApi( api = Build.VERSION_CODES.M)
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 101){
            for (int i = 0; i < grantResults.length; i++){
                if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                    if(shouldShowRequestPermissionRationale(permissions[i])){
                        new AlertDialog.Builder(this)
                                .setMessage("Estos permisos son necesarios para establecer una foto de contacto")
                                .setPositiveButton("Aceptar", (dialog, which) -> pedirVariosPermisos())
                                .setNegativeButton("Cancelar", (dialog, which) -> dialog.dismiss())
                                .create()
                                .show();
                    }
                    return;
                }
            }

        }

    }

    /**
     * Cuando recibe el resultado de la actividad recoge la información y la muestra en el imageView
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch(requestCode){
            case INTENT_CAMARA:
                if (requestCode == INTENT_CAMARA && resultCode == RESULT_OK) { ;
                    Bundle extras = data.getExtras();
                    Bitmap imageBitmap = (Bitmap) extras.get("data");
                    ImageView imgFoto = (ImageView) findViewById(R.id.imgFotoEdit);
                    imgFoto.setImageBitmap(imageBitmap);
                }
                break;
            case INTENT_GALERIA:
                if((requestCode == INTENT_GALERIA) && (resultCode == RESULT_OK) && (data != null)){
                    Uri imagenSeleccionada = data.getData();
                    String[] ruta = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContentResolver().query(imagenSeleccionada, ruta, null, null);
                    cursor.moveToFirst();

                    int indice = cursor.getColumnIndex(ruta[0]);
                    String rutaFoto = cursor.getString(indice);
                    cursor.close();

                    ImageView img = (ImageView) findViewById(R.id.imgFotoEdit);
                    img.setImageBitmap(BitmapFactory.decodeFile(rutaFoto));
                }
                break;
        }

    }

    /**
     * Crea un archivo para las fotos sacadas con la cámara.
     * @return
     * @throws IOException
     */
    private File createImageFile() throws IOException{
        String hora = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String nombre = "JPEG_" + hora + "_";
        File direccionAlmacenamiento = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File imagen = File.createTempFile(nombre, ".jpg", direccionAlmacenamiento);

        direccionActualFoto = imagen.getAbsolutePath();

        return imagen;
    }
</pre>

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.agendaandroid">
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <uses-feature android:name="android.hardware.camera"
        android:required="false" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">



        <activity android:name=".DetallePersona"></activity>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="com.example.agendaandroid.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>

        <activity
            android:name=".NuevaPersonaActivity"
            android:label="@string/title_activity_nueva_persona"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

还有我的file_paths:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Android/data/com.example.agendaandroid/files/Pictures" />
</paths>

拍照效果很好,但是一旦我添加了代码来为照片创建文件,activity 在拍照后关闭并在 运行 上显示错误。

希望这些信息足够了,非常感谢。

编辑: 我决定将照片存储在应用程序的目录中以使其更简单,但事实证明我传递给 onActivityResult 的 Intent 为 null,因此无法获取额外内容。

      Intent intentFoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intentFoto.resolveActivity(getPackageManager()) != null) {
            File foto = null;
            try{
                foto = createImageFile();
            } catch (IOException e){
                e.printStackTrace();
            }
            if(foto != null){
                Uri uriFoto = FileProvider.getUriForFile(getApplicationContext(),
                        "com.example.agendaandroid.fileprovider",
                        foto);
                intentFoto.putExtra(MediaStore.EXTRA_OUTPUT, uriFoto);
                startActivityForResult(intentFoto, INTENT_CAMARA);
            }

        }
                    Uri uriFoto = FileProvider.getUriForFile(NuevaPersonaActivity.this, "com.example.agendaandroid.fileprovider", foto);
                    intentFoto.putExtra(MediaStore.EXTRA_OUTPUT, uriFoto);
                    startActivityForResult(intentFoto, INTENT_CAMARA);

您正在使用 EXTRA_OUTPUT。您的照片应写入您提供的位置。

                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");

您正在使用 EXTRA_OUTPUT。不应该有任何多余的东西。您的照片应写入您在 EXTRA_OUTPUT 额外内容中提供的位置。使用您最喜欢的图像加载库(例如 Glide、Picasso)将其加载到您的 ImageView.

最后从图库或相机拍照的方法是这样的:

 private void dialogoFoto() {
        AlertDialog.Builder builder = new AlertDialog.Builder(NuevaPersonaActivity.this);
        builder.setTitle("Añadir una foto").setPositiveButton(R.string.camara, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, INTENT_CAMARA);
            }
        }).setNegativeButton(R.string.galeria, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intentGaleria = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intentGaleria, INTENT_GALERIA);
            }
        });

        builder.show();
    }

onActivityResult 是这样的:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch(requestCode){
            case INTENT_CAMARA:
                if (requestCode == INTENT_CAMARA && resultCode == RESULT_OK) {
                    Bitmap photo = (Bitmap) data.getExtras().get("data");

                    ImageView imgFoto = (ImageView) findViewById(R.id.imgFotoEdit);
                    imgFoto.setImageBitmap(photo);
                }
                break;
            case INTENT_GALERIA:
                if((requestCode == INTENT_GALERIA) && (resultCode == RESULT_OK) && (data != null)){
                    Uri imagenSeleccionada = data.getData();
                    String[] ruta = {MediaStore.Images.Media.DATA};

                    Cursor cursor = getContentResolver().query(imagenSeleccionada, ruta, null, null);
                    cursor.moveToFirst();

                    int indice = cursor.getColumnIndex(ruta[0]);
                    String rutaFoto = cursor.getString(indice);
                    cursor.close();

                    ImageView img = (ImageView) findViewById(R.id.imgFotoEdit);
                    img.setImageBitmap(BitmapFactory.decodeFile(rutaFoto));
                }
                break;
        }

    }