简体   繁体   English

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

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

I am making a contact agenda app and I need to save the photos taken with the camer into the device gallery. 我正在制作一个联系议程应用程序,我需要将使用相机拍摄的照片保存到设备库中。 I've been following this tutorial: https://developer.android.com/training/camera/photobasics and looked and tried several fixes, but none seem to work. 我一直在关注这个教程: https//developer.android.com/training/camera/photobasics并查看并尝试了几个修复程序,但似乎都没有。

Here is the error thrown after taking the picture: 这是拍照后抛出的错误:

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) 

this is my NuevaPersonaActivity.java: 这是我的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://stackoverflow.com/questions/10165302/dialog-to-pick-image-from-gallery-or-from-camera
        //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>

My manifest: 我的清单:

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

And my file_paths: 我的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>

Taking photos works just fine, but once I add the code to create the file for the photograph the activity closes after taking the photo and said error appears on Run. 拍摄照片效果很好,但是一旦我添加代码来创建照片文件,拍摄照片后活动就会关闭,并且运行时出现错误。

I hope all that info is enough, and thank you very much. 我希望所有这些信息都足够了,非常感谢你。

Edit: I've decided to store the photos in the app's directory to keep it simpler, but it turns out that the Intent I pass into the onActivityResult is null, so it can't get the extras. 编辑:我已决定将照片存储在应用程序的目录中以使其更简单,但事实证明我传入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);

You are using EXTRA_OUTPUT . 您正在使用EXTRA_OUTPUT Your photo should be written to the location that you provided. 您的照片应写入您提供的位置。

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

You are using EXTRA_OUTPUT . 您正在使用EXTRA_OUTPUT There should not be any extras. 不应该有任何额外的东西。 Your photo should be written to the location that you provided in the EXTRA_OUTPUT extra. 您的照片应写入您在EXTRA_OUTPUT额外提供的位置。 Use your favorite image-loading library (eg, Glide, Picasso) to load it into your ImageView . 使用您喜欢的图像加载库(例如,Glide,Picasso)将其加载到ImageView

In the end the method to take a photo either from gallery or from the camera looks like this: 最后,从图库或相机拍摄照片的方法如下所示:

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

And the onActivityResult like this: 而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;
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM