Android startCamera gibt mir null Intent und ... zerstört es meine globale Variable?
Ich habe das nächste Problem:
Wenn ich versuche, meine Kamera zu starten, kann ich das Bild aufnehmen oder sogar auf meiner SD-Karte speichern, aber wenn ich den Pfad zum Anzeigen auf meinem Gerät erhalte, erhalte ich Fehler.
Meine globalen Variablen sind 2 (ich habe 1 verwendet, aber 2 ist besser, um sicherzustellen, dass es sich um einen seltsamen Fehler handelt):
private File photofile;
private Uri mMakePhotoUri;
und das ist meine start-kamerafunktion:
@SuppressLint("SimpleDateFormat")
public void farefoto(int num){
// For naming the picture
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String n = sdf.format(new Date());
String fotoname = "Immagine-"+ n +".jpg";
//Going through files and folders
File photostorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File photostorage2 = new File(photostorage, "Immagini");
System.out.println(photostorage+"\n"+photostorage2);
photostorage2.mkdirs();
// My file (global)
photofile = new File(photostorage2, fotoname);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //intent to start camera
// My URI (global)
mMakePhotoUri = Uri.fromFile(photofile);
new Bundle(); // I took this code from internet, but if I remove this line, it's the same
i.putExtra(MediaStore.EXTRA_OUTPUT, mMakePhotoUri);
startActivityForResult(i, num); //num would be 1 on calling function
}
und mein Aktivitätsergebnis:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == 1){
try{ // tring my global URI
photo = f.decodeAndResizeFile(new File(mMakePhotoUri.getPath()));
}
catch(NullPointerException ex){
System.out.println("fail");
ex.printStackTrace();
try{ // Trying my global FILE
photo = BitmapFactory.decodeFile(photofile.getAbsolutePath());
} catch (Exception e){
e.printStackTrace();
Toast.makeText(this, "C'è stato un errore. Riprova a scattare la foto.", Toast.LENGTH_LONG).show();
}
......
......
.......
}
Immer NullPointerException erhalten
Aber...Wenn ich das Bild erneut mache, FUNKTIONIERT ES !!.
Ich habe hier alles gelesen, was ich konnte ... aber es hat keine Logik, wenn ich eine globale Variable ändere und ich kann sie nicht wieder aufnehmen ...
Danke im Voraus. Prost.
LÖSUNGWieAlex Cohn sagte, mein Problem war, dass ich anriefonCreate
VoronActivityResult
Wegen eines möglichen Speichermangels (weil es manchmal nicht geht), wollte ich meine App "gesund" haben und habe einige ausprobierttry / catch
und so bekomme ich die daten auch wenn sie anrufenonCreate
oderonActivityResult
Für den ersten Aufruf habe ich diese Daten in ein Bundle geschrieben, wie im Link von erklärtWiederherstellung unseres Staates.
Vielen Dank!.