2016-11-10 30 views
0

我只是想製作一個使用運行時權限的簡單相機應用程序......它似乎在第一次應用程序發射(請求相機許可)。允許訪問後,它可以正常工作......但是一旦我關閉它並重新啓動它,它就會顯示一個白色圖像,並且我的圖標不會響應。我已經在應用程序中手動檢查權限,相機仍然被授予訪問權限,但我認爲我搞砸了我的權限代碼。我的簡單相機應用程序(android)不會在第一次嘗試後加載相機視圖

這裏是在MainActivity代碼:

public class MainActivity extends AppCompatActivity { 

    private static final int MY_PERMISSIONS_REQUEST_CAMERA = 1; 
    private Camera mCamera = null; 
    private Camera mCameraFront = null; 
    private CameraView mCameraView = null; 
    public int switchCamera = 1; 


// int permissionCheck = ContextCompat.checkSelfPermission(this, 
//   Manifest.permission.CAMERA); 

// String[] perms = {"android.permission.CAMERA"}; 


// int permsRequestCode = 200; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.CAMERA) 
       != PackageManager.PERMISSION_GRANTED) { 

      // Should we show an explanation? 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        Manifest.permission.CAMERA)) { 

       // Show an explanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 

      } else { 

       // No explanation needed, we can request the permission. 

       ActivityCompat.requestPermissions(this, 
         new String[]{Manifest.permission.CAMERA}, 
         MY_PERMISSIONS_REQUEST_CAMERA); 

       try { 
        mCamera = Camera.open(1);//you can use open(int) to use different cameras 
       } catch (Exception e) { 
        Log.d("ERROR", "Failed to get camera: " + e.getMessage()); 
       } 
       SwapCamera(); 

//    if (mCamera != null) { 
////      mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data 
////      FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view); 
////      camera_view.addView(mCameraView);//add the SurfaceView to the layout 
//     SwapCamera(); 
//    } 

       //btn to close the application 
       ImageButton imgClose = (ImageButton) findViewById(R.id.imgClose); 
       imgClose.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         mCamera.setPreviewCallback(null); 
         mCamera.setErrorCallback(null); 
         mCamera.stopPreview(); 
         mCamera.release(); 
         mCamera = null; 
         System.exit(0); 
        } 
       }); 

//      btn to switch camera 
       ImageButton imgSwitch = (ImageButton) findViewById(R.id.cameraSwitch); 
       imgSwitch.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
//       switchCamera++; 
        } 
       }); 


      } 
     } 
    } 



    public void SwapCamera() { 
     mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data 
     FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view); 
     camera_view.addView(mCameraView);//add the SurfaceView to the layout 
    } 

    @Override 
    public void onRequestPermissionsResult(int requestCode, 
              String permissions[], int[] grantResults) { 

     switch (requestCode) { 
      case MY_PERMISSIONS_REQUEST_CAMERA: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 



        // permission was granted, yay! Do the 
        // camera-related task you need to do. 

        try{ 
         mCamera = Camera.open(1);//you can use open(int) to use different cameras 
        } catch (Exception e){ 
         Log.d("ERROR", "Failed to get camera: " + e.getMessage()); 
        } 

        if(mCamera != null) { 
//      mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data 
//      FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view); 
//      camera_view.addView(mCameraView);//add the SurfaceView to the layout 
         SwapCamera(); 
        } 

        //btn to close the application 
        ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose); 
        imgClose.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
          mCamera.setPreviewCallback(null); 
          mCamera.setErrorCallback(null); 
          mCamera.stopPreview(); 
          mCamera.release(); 
          mCamera = null; 
          System.exit(0); 
         } 
        }); 

//      btn to switch camera 
        ImageButton imgSwitch = (ImageButton)findViewById(R.id.cameraSwitch); 
        imgSwitch.setOnClickListener(new View.OnClickListener() { 
         @Override 
         public void onClick(View view) { 
//       switchCamera++; 
         } 
        }); 



       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
       } 
       return; 
      } 

      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 

    public void onActivityResult() { 

    } 


} 

回答

1

我相信你缺少「其他」

if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.CAMERA) 
       != PackageManager.PERMISSION_GRANTED) { 
// your code 

} else { 
    // stuff with camera 
    try{ 
     mCamera = Camera.open(1); 
    } catch (Exception e){ 
    ... 
} 
+0

我很困惑,我很想念了「別人」 ......我與onRequestPermissionsResult方法我「嘗試」塊... 。我應該將它移動到onCreate方法嗎? (在「if(ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)」區域中) – fmi

+0

您應該將其複製到onCreate方法。 @弗拉德馬特維科解釋道。 – innich

1

你只開始onActivityResult相機的工作人員,只有當你沒有被稱爲權限,並要求他們:

if (ContextCompat.checkSelfPermission(this, 
       Manifest.permission.CAMERA) 
       != PackageManager.PERMISSION_GRANTED) { 
    ... 
} 

if需要else,如果權限已經是PERMISSION_GRANTED。你必須在這裏做你在onActivityResult做同樣的工作人員:

else { 
    try{ 
          mCamera = Camera.open(1);//you can use open(int) to use different cameras 
         } catch (Exception e){ 
          Log.d("ERROR", "Failed to get camera: " + e.getMessage()); 
         } 

         if(mCamera != null) { 
    //      mCameraView = new CameraView(this, mCamera);//create a SurfaceView to show camera data 
    //      FrameLayout camera_view = (FrameLayout)findViewById(R.id.camera_view); 
    //      camera_view.addView(mCameraView);//add the SurfaceView to the layout 
          SwapCamera(); 
         } 

         //btn to close the application 
         ImageButton imgClose = (ImageButton)findViewById(R.id.imgClose); 
         imgClose.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View view) { 
           mCamera = null; 
           System.exit(0); 
          } 
         }); 

    //      btn to switch camera 
         ImageButton imgSwitch = (ImageButton)findViewById(R.id.cameraSwitch); 
         imgSwitch.setOnClickListener(new View.OnClickListener() { 
          @Override 
          public void onClick(View view) { 
    //       switchCamera++; 
          } 
         }); 
} 
+0

我已將try塊複製到else語句中(只是超出了!= PERMISSION_GRANTED),但我仍然得到相同的結果......我想我誤解了你的答案 – fmi

+0

@fmi,你會得到任何異常? –

+0

不是....第一次加載應用程序時,它會提示我提出請求....前置攝像頭然後顯示2個圖標uptop ....關閉應用程序並重新打開後,我沒有得到提示了,我只是看到一個白色屏幕上的2個圖標,將不會作出任何反應 – fmi

相關問題