2016-05-05 6 views
0

写真を撮る必要があります。フルサイズのファイルをサーバーに送信する必要があります。サムネイルでは正常に動作しますが、フルサイズの写真は復元できません。私はandroid developers web pageのgoogleチュートリアルのコードのほとんどを読み、コピーしました。写真を撮った後のヌルの結果

私はこれやってる:

private void dispatchTakePictureIntent() { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) { 
     mPhotoFile = null; 
     try { 
      mPhotoFile = createImageFile(); 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     }    
     if (mPhotoFile != null) { 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, 
        mCurrentPhotoPath); 
      startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO); 
     } 
    } 
} 

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp; 
    File storageDir; 
    if (StorageUtils.isExternalStorageWritable()) { 
     storageDir = StorageUtils.getExternalStorageAppDir(getActivity()); 
    } else { 
     storageDir = Environment.getDataDirectory(); 
    } 
    File image = File.createTempFile(
      imageFileName, 
      ".jpg", 
      storageDir 
    ); 
    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inJustDecodeBounds = true;   
     Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); // returns null 
     mImageAdapter.addImage(bitmap); 
    } 
} 

この行(onActivityResultはnullを返し内側):

Bitmap bitmap= BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 

を私はカメラの問題に関する記事をたくさん読んで、何も動作していないようにみえます。私は何か間違っている?

ありがとうございます。

注:私はエミュレータと実際のデバイスでコードをテストします。同じ結果。

+0

保存されたインスタンス状態「バンドル」に 'mCurrentPhotoPath'を保存していることを確認してください。 https://stackoverflow.com/questions/37039085/android-inconsistent-picture-saving-with-getexternalfilesdir/37039274を参照してください。 – CommonsWare

+0

はい、私はそれを保存しています。画像はmCurrentPhotoPathで生成されないので、おそらく問題はファイルの作成です。 – Pablo

答えて

0

問題は、この行にあった:

bmOptions.inJustDecodeBounds = true; 

bmOptions.inJustDecodeBoundsについてGoogleドキュメントは言う:

trueに設定した場合、デコーダはヌル(無ビットマップ)を返しますが、します out ...フィールドがまだ設定されているため、呼び出し元はピクセルのメモリを割り当てることなくビットマップ を照会できます。

この行を削除すると、イメージが正常に返されました。

関連する問題