2016-10-28 5 views
1

私のアンドロイドアプリケーションでは、ボタンを最初にクリックするとカメラが開き、画像をキャプチャすると新しい動作にリダイレクトされます。ボタンをクリックするまでカメラが開きません - android

しかし、私が最初にボタンをクリックすると、新しいアクティビティにリダイレクトされ、戻るボタンをクリックするとカメラが開きます。

私のコードで何が問題なのか分かりませんが、うまくできているようです。助けてください。

public void onCameraButtonClicked() 
    { 

     camera_btn.setOnClickListener(
       new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

         final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; 
         makingDirectory(dir); 

         storePicture(dir); 
        } 
       } 
     ); 
    } 

    public void makingDirectory(String dir) 
    { 
     File newdir = new File(dir); 
     newdir.mkdirs(); 
    } 

    public void storePicture(String dir) 
    { 
     boolean flag_storePicture = false; 
     // Here, the counter will be incremented each time, and the 
     // picture taken by camera will be stored as 1.jpg,2.jpg 
     // and likewise. 
     count++; 
     String file = dir+count+".jpg"; 
     File newfile = new File(file); 
     try 
     { 
      newfile.createNewFile(); 
      Uri outputFileUri = Uri.fromFile(newfile); 

      Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 

      startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); 
      flag_storePicture = true; 
      Log.d("myApp", "Hiiiiii from inside"); 
     } 
     catch (IOException e) 
     { 

     } 
     finally { 
      Log.d("myApp", "Hiiiiii"); 
      Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera"); 
      startActivity(camera_intent); 
     } 
    } 

私はこのメッセージを受け取っています。

Skipped 62 frames! The application may be doing too much work on its main thread. 

答えて

2

それを明確にあなたにだけでなく、問題は、制御の流れがあり、tryブロック内の

は、cameraアプリは右そのfinallyあと

startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); 

のために開かれています実行される

finally { 
      Log.d("myApp", "Hiiiiii"); 
      Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera"); 
      startActivity(camera_intent); 
     } 

あなたは012を持っていますあなたのカメラアプリを介しての活動ので、したがって、あなたはあなたのカメラアプリを見つけることを押してください。

解決方法:必要に応じてfinallyのブロックコードをonActivityResultと一緒にifの条件に移動して、画像キャプチャタスクの完了を確認します。 like

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK){ // image captured successfully 
      //..code to start your other activity 
     Intent camera_intent = new Intent("com.example.lalinda.googlemap1.Camera"); 
     startActivity(camera_intent); 
     }else{ 
      // .. image capture failure , user pressed cancel etc 
      } 
    } 
関連する問題