2016-04-11 6 views
1

Ich versuche, das Miniaturbild des aufgenommenen Bildes in das scr eines ImageButtons zu setzen. Der folgende Code funktioniert gut, wenn das Bild aufgenommen und in der Galerie gespeichert wird, aber die "Bitmap imageBitmap = (Bitmap) extras.get (" data ");" gibt null zurück. Kann mir bitte jemand erklären warum?Daten, die bei der Aufnahme eines Bildes als Null zurückgegeben wurden

private void openCamera() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    // Ensure that there's a camera activity to handle the intent 
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { 
     // Create the File where the photo should go 
     photoFile = null; 
     try { 
      photoFile = createImageFile(); 
     } catch (IOException ex) { 
      // Error occurred while creating the File 
      Log.i("Camera log", "Failed:" + ex.toString()); 
     } 
     // Continue only if the File was successfully created 
     if (photoFile != null) { 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
        Uri.fromFile(photoFile)); 

      startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
     } 
    } 
} 

private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(mCurrentPhotoPath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
} 


@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     Bitmap imageBitmap = (Bitmap) extras.get("data"); 
     cameraBtn.setImageBitmap(imageBitmap); 
     galleryAddPic(); 
    } 
} 
+1

Siehe 'photoFile' Objekt, das Bild erfasst, enthalten. –

Antwort

1

Wenn Sie eine MediaStore.EXTRA_OUTPUT einstellen, müssen Sie das Foto von dieser URL erhalten.

Beispiel:

if(photoFile!=null){ 
BitmapFactory.Options bmOptions = null; 
bmOptions = new BitmapFactory.Options(); 
Bitmap mBitmap = BitmapFactory.decodeFile(photoFile.getPath(), bmOptions); 
} 
else{ 
Bundle extras = data.getExtras(); 
     Bitmap imageBitmap = (Bitmap) extras.get("data"); 
     cameraBtn.setImageBitmap(imageBitmap); 
     galleryAddPic(); 
} 
+0

Danke Miguel. Durch das Hinzufügen von 'getPath()' zu decodeFile (photoFile, bmOptions) in Ihrer Antwort wurde es gelöst. Sehr geschätzt. :) –

0

Kann jemand bitte erklären, warum?

Sie haben EXTRA_OUTPUT bereitgestellt. Ihr Bild sollte an dem von Ihnen angegebenen Ort gespeichert werden. Wenn Sie nicht liefern EXTRA_OUTPUT, dannextras.get("data") sollten Sie Ihre Bitmap haben (zumindest, wenn die Kamera-App Ihre App mit Anwendungen hat keine Bugs).