2016-04-11 9 views
1

撮影した画像のサムネイルをImageButtonのscrに設定しようとしています。次のコードは、写真を撮ってギャラリーに保存しても問題なく動作しますが、 "Bitmap imageBitmap =(Bitmap)extras.get(" data ");" nullを返します。 誰でも理由を説明できますか?画像を撮影するときにnullとして返されるデータ

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

は、撮像画像を含む 'photoFile'オブジェクトを参照して下さい。 –

答えて

1

MediaStore.EXTRA_OUTPUTを設定すると、このURLから写真を取得する必要があります。

例:

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

Miguelに感謝します。あなたの答えの中でdecodeFile(photoFile、bmOptions)に 'getPath()'を追加することで解決しました。とても有難い。 :) –

0

誰もが理由を説明していただけますか?

EXTRA_OUTPUTを指定しました。あなたのイメージはあなたが指定した場所に保管されるべきです。 でない場合、EXTRA_OUTPUTextras.get("data")のいずれかがBitmapである必要があります(少なくとも使用しているカメラアプリにバグがない場合)。

関連する問題