我正在尝试通过按钮更改为其他活动。第二个活动(称为capturar)拍摄照片并将其保存在图库中。 但是,由于某种原因,每次我尝试使用该按钮更改为该活动时,它都会崩溃,我知道问题出在capturar的代码上,因为如果我注释所有代码,它将起作用。
我要放置一张我的主要活动代码的图片(在这里我试图有意地更改为其他活动)和我的Capturar活动的代码在其中我尝试拍照并保存。
主要活动:
movdqa
主要活动xml
package com.iris.phototask;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
//Declaramos las variables
public Button btnHorario;
public Button Galeria;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Asignamos el componente al que corresponde la variable
btnHorario = (Button) findViewById(R.id.buttonHorario);
ImageButton imgButtonCamara = findViewById(R.id.imageButtonCamera);
Galeria = (Button) findViewById(R.id.buttonGaleria);
//Creamos un onClickListener para generar un cambio de actividad al pulsar el botón
btnHorario.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creamos un Intent que nos cambie de ventana
Intent intent = new Intent (MainActivity.this, Horario.class);
startActivity(intent);
}
});
imgButtonCamara.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Creamos un Intent que nos cambie de ventana
Intent intent = new Intent (MainActivity.this, Capturar.class);
startActivity(intent);
}
});
}
}
捕获活动
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/buttonGaleria"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="30dp"
android:background="#8BC34A"
android:text="Galeria"
app:layout_constraintBottom_toTopOf="@+id/buttonHorario"
app:layout_constraintEnd_toEndOf="@+id/buttonHorario"
app:layout_constraintStart_toStartOf="@+id/buttonHorario"
app:layout_constraintTop_toBottomOf="@+id/imageButtonCamera" />
<Button
android:id="@+id/buttonHorario"
android:layout_width="128dp"
android:layout_height="0dp"
android:layout_marginBottom="234dp"
android:background="#8BC34A"
android:text="Horario"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buttonGaleria" />
<ImageButton
android:id="@+id/imageButtonCamera"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="175dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="32dp"
android:background="#8BC34A"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toTopOf="@+id/buttonGaleria"
app:layout_constraintEnd_toEndOf="@+id/buttonGaleria"
app:layout_constraintStart_toStartOf="@+id/buttonHorario"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_camera" />
</androidx.constraintlayout.widget.ConstraintLayout>
Capturar xml
package com.iris.phototask;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.UUID;
public class Capturar extends AppCompatActivity {
//Declaración de variables
private static final String TAG = "CapturePicture";
static final int REQUEST_PICTURE_CAPTURE = 1; //Código para referirnos a request capture
private ImageView Imagen; //Declaramos el imageview que tenemos en el xml
private String deviceIdentifier;
String currentPhotoPath; //Un string donde guardaremos el url de la foto
Uri photoURI; //El uri de la foto
@Override
protected void onCreate(Bundle savedInstanceState) {// ************************************************* INICIA ON CREATE PRINCIPAL ********************************************************
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capturar);
Imagen = findViewById(R.id.imageViewImagen);
Button guardarButton = findViewById(R.id.buttonGuardar);
Button captureButton = findViewById(R.id.buttonCapture);
captureButton.setOnClickListener(buttonCapture);
guardarButton.setOnClickListener(buttonGuardar);
//Si no se habilitó la cámara, deshabilitar el botón de capturar
if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
captureButton.setEnabled(false);
}
findViewById(R.id.save_local).setOnClickListener(buttonGuardar);
getInstallationIdentifier();
}// *************************************************************************************************** TERMINA ON CREATE PRINCIPAL *********************************************************
//Si se habilitó la cámara y se pulsa el botón para capturar foto, llamamos al intent que toma la foto, declarado más adelante
private View.OnClickListener buttonCapture = new View.OnClickListener(){
@Override
public void onClick(View view){
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
dispatchTakePictureIntent();
}
}
};
//Creamos una función que genera un intent que solicita IMAGE_CAPTURE
private void dispatchTakePictureIntent(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Asegurarse de que hay un camera activity para manejar el intent
if(takePictureIntent.resolveActivity(getPackageManager())!=null){//Inicia if
//Creamos el archivo a donde la foto irá
File photoFile = null;
try{//Inicia Try Catch
//Llamammos a la función para crear la foto
photoFile = createImageFile();
}catch (IOException ex){
//Un error ocurrió al crear el archivo
}//Termina catch
//Si foto file es diferente de nulo
if (photoFile != null){//Empieza if 2
photoURI = FileProvider.getUriForFile(this,"com.example.android.fileprovider",photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent,REQUEST_PICTURE_CAPTURE);
}//Termina if 2
}//Termina if
}
private File createImageFile() throws IOException {//Creamos la función que generará la imagen
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//SI no existe en directorio para almacenarlo, lo crea.
if (!storageDir.exists())
storageDir.mkdir();
File Imagen = File.createTempFile(
"2019",".jpeg",storageDir
);
currentPhotoPath = Imagen.getAbsolutePath();
return Imagen;
}//Termina función para crear la imagen
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){//Función de iniciar el INTENT con resultados
if (requestCode == REQUEST_PICTURE_CAPTURE && resultCode == RESULT_OK){//Inicia if
Bitmap bitmap;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),photoURI);
Imagen.setImageBitmap(bitmap);
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}//Termina if
}//Termina función de iniciar el INTENT con resultados
//AL presionar el botón guardar la intentaremos guardar en la librería
private View.OnClickListener buttonGuardar = new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
addToGallery();
}catch (IOException e){
e.printStackTrace();
}
}
};
//Iniciamos la función de añadir a la galería
private void addToGallery() throws IOException{
Intent mediaScanIntent = new Intent (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File (currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
notifyMediaStoreScanner(f); //función
}
public final void notifyMediaStoreScanner (final File file){
try{
MediaStore.Images.Media.insertImage(this.getContentResolver(),file.getAbsolutePath(), file.getName(),null);
this.sendBroadcast(new Intent (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
}catch (FileNotFoundException e){
e.printStackTrace();
}
}
protected synchronized String getInstallationIdentifier(){
if(deviceIdentifier == null){
SharedPreferences sharedPref = this.getSharedPreferences("DEVICE_ID", Context.MODE_PRIVATE);
deviceIdentifier = sharedPref.getString("DEVICE_ID", null);
if(deviceIdentifier == null){
deviceIdentifier = UUID.randomUUID().toString();
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString("DEVICE_ID",deviceIdentifier);
editor.commit();
}
}
return deviceIdentifier;
}
}
控制台:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Capturar">
<ImageView
android:id="@+id/imageViewImagen"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="45dp"
android:layout_marginTop="73dp"
android:layout_marginEnd="45dp"
android:layout_marginBottom="73dp"
app:layout_constraintBottom_toTopOf="@+id/buttonCapture"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@android:drawable/ic_menu_gallery" />
<Button
android:id="@+id/buttonCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="51dp"
android:text="Capturar"
android:background="#8BC34A"
app:layout_constraintBottom_toBottomOf="@+id/buttonGuardar"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/buttonGuardar" />
<Button
android:id="@+id/buttonGuardar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="52dp"
android:layout_marginBottom="78dp"
android:text="Guardar"
android:background="#8BC34A"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
非常感谢,我希望自己能自己解释一下。
答案 0 :(得分:0)
空指针异常是由于以下行引起的
findViewById(R.id.save_local).setOnClickListener(buttonGuardar);
因为找不到ID save_local