2017-10-29 16 views
1

私はかなりAndroid開発に慣れていますが、ボタンを1つクリックして暗黙のインテントを呼び出すアプリケーションを作成しようとしています。画像がキャプチャされたら、戻るボタンをクリックしてメインアクティビティにアクセスできます。主な活動では、あなたがそれをクリックしたときに、あなたが最近使用したファイルを見ることができますし、撮影した画像は、私がイメージにカメラでキャプチャした画像は保存されていません

を取得するための次のコードを使用しhttps://developer.android.com/training/camera/photobasics.html#TaskScalePhoto

て働いていたが

を示すべきであることを第二のボタンがあります最終的なTextView textviewcamera =(TextView)findViewById(R.id.TextView1); final int REQUEST_IMAGE_CAPTURE = 1;

// Set an OnClickListener on this Text View 
    // Called each time the user clicks the Text View 
    textviewcamera.setOnClickListener(new View.OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
     /* 
      opens the camera app and we are able to take a photo, photo is not save anywhere, needs to be fixed 
      code is from android studio, DON'T FORGET to cite 
      https://developer.android.com/training/camera/photobasics.html 

     */ 
      Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      //if (takePictureIntent.resolveActivity(getPackageManager()) != null) 
      //{ 
       startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); 
      //} 

     } 

その後、私は最近のファイルを

最終のTextViewのtextviewpicture =(のTextView)findViewById(R.id.TextView2)を示すコードの別の部分を持っています。

// Set an OnClickListener on this Text View 
    // Called each time the user clicks the Text View 
    textviewpicture.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     /* 
      opens the camera app and we are able to take a photo, photo is not save anywhere, needs to be fixed 
      code is from android studio, DON'T FORGET to cite 
      https://developer.android.com/training/camera/photobasics.html 

     */ 
      Intent viewpicture = new Intent(); 
      viewpicture.setType("image/*"); 
      viewpicture.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(viewpicture, "Select Picture"), 10); 
     } 
    }); 

カメラを開いて写真を撮ることはできますが、最近のファイルで見ることができます。この部分は機能しません。

すべてのヘルプは非常ミル皆:)

+0

'ACTION_IMAGE_CA PTURE'はあなたのアプリに 'onActivityResult()'コールでイメージを送ります。 'ACTION_IMAGE_CAPTURE'' Intent'に 'EXTRA_OUTPUT'を入れていないので、サムネイル' Bitmap'が得られます。この画像はデバイスに保存する必要はありません—実際には、それはおそらくカメラアプリのバグです。したがって、カメラアプリは画像を保存していないので、画像を保存していないので、他のアプリはその画像を見ることができません。 – CommonsWare

+0

私に戻ってくれてありがとう、ありがとう。 EXTRA_OUTPUTに何を追加すればよいので、画像はギャラリーに保存されます。 intent.putExtra(MediaStore.EXTRA_OUTPUT、capturedImageUri)を追加しようとしました。私はちょうどcaptureImageUriのエラーを取得しています...私はそこに入力することができますので、画像はギャラリーに保存されますか? – Ingells

+0

私はエラーが何であるか分からないので、私はあなたにそれに関する多くのアドバイスを提供することはできません。 [EXTRA_OUTPUT]の使用方法を示す[サンプルアプリケーションはこちら](https://github.com/commonsguy/cw-omnibus/tree/v8.7/Camera/FileProvider)です。 'FileProvider'によって管理される場所です。 – CommonsWare

答えて

0

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    switch (requestCode) { 
case REQUEST_IMAGE_CAPTURE: 
      if (resultCode != RESULT_CANCELED) 
      { 
      if (resultCode == RESULT_OK) { 

       BitmapFactory.Options options = new 
       BitmapFactory.Options(); 
       options.inSampleSize = 8; 
     Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(), 
       options); 
      saveImageToExternalStorage(bitmap) 
      } 
    } else{ 
    //show error message 
    } 
    } } 

public static File saveImageToExternalStorage(Bitmap finalBitmap) { 
    File file; 
    String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString(); 
    File myDir = new File(root + "/easyGovs"); 
    myDir.mkdirs(); 
    Random generator = new Random(); 
    int n = 10000; 
    n = generator.nextInt(n); 
    String fname = "Image-" + n + ".png"; 
    file = new File(myDir, fname); 
    if (file.exists()) 
     file.delete(); 
    try { 
     FileOutputStream out = new FileOutputStream(file); 
     finalBitmap.compress(Bitmap.CompressFormat.PNG, 55, out); 
     out.flush(); 
     out.close(); 
     return file; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return file; 
} 

と通過する間に外部ストレージに保存しますが、そこから自分の画像をプレビューすることができますが、あなたのonActivityresultを上書き

おかげでいただければ幸いですこのカメラ用のあなたの意図 -

 private Uri fileUri; 
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    // start the image capture Intent 
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
関連する問題