2017-09-30 143 views
1

這裏是我的代碼:手電筒應用程序無法連接到相機服務

我添加了一個github上許可的代碼,但它仍然崩潰

我所做的每一件事,但它崩潰每一次

我也有我的清單

    parameter = camera.getParameters(); 
       } 
       @Override public void onPermissionDenied(PermissionDeniedResponse response) { 
        Toast.makeText(getApplicationContext(), "Permission Denied", Toast.LENGTH_SHORT).show(); 
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
        builder.setMessage("App needs permission to access camera") 
          .setPositiveButton("Granted", new DialogInterface.OnClickListener() { 
           public void onClick(DialogInterface dialog, int id) { 
            Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + getPackageName())); 
            myAppSettings.addCategory(Intent.CATEGORY_DEFAULT); 
            myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            startActivity(myAppSettings); 

           } 
          }).setNegativeButton("Denied", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int id) { 
         } 
        }).create().show(); 
       } 
       @Override public void onPermissionRationaleShouldBeShown(PermissionRequest permission, PermissionToken token) 
       {[enter image description here][1] 
        token.continuePermissionRequest(); 
       } 

      }).check(); 





    //getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 
    textview = (TextView) findViewById(R.id.textView); 
    flashLight = (ImageButton) findViewById(R.id.flash_light); 

// setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)添加權限相機;

//askPermission(CAMERA,camera1); 






    flashLight.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (!isFlashLightOn) { 
       turnOnTheFlash(); 
      } else { 
       turnOffTheFlash(); 
      } 
     } 
    }); 

logcat的:

09-30 18:59:31.698 11339-11339/inducesmile.com.androidflashlightapp E/AndroidRuntime:致命異常:主 工藝:inducesmile.com.androidflashlightapp,PID:11339 java.lang.RuntimeException:無法恢復活動{inducesmile.com.androidflashlightapp/inducesmile.com.androidflashlightapp.MainActivity}:java.lang.RuntimeException:無法連接到攝像頭服務 at android.app.ActivityThread.performResumeActivity(ActivityThread .java:3506) android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) app.ActivityThread $ H.handleMessage(ActivityThread.java:1527) 在android.os.Handler.dispatchMessage(Handler.java:110) 在android.os.Looper.loop(Looper.java:203) 的機器人。 app.ActivityThread.main(ActivityThread.java:6251) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit .java:1073) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 引起:java.lang.RuntimeException:無法連接到相機服務 at android.hardware.Camera。( Camera.java:647) 在android.hardware.Camera.open(Camera.java:510) 在inducesmile.com.androidflashlightapp.MainActivity.turnOffTheFlash(MainActivity.java:105) 在inducesmile.com.androidflashlightapp.MainActivity。 onResume(MainActivity.java:165) at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1269) at android.app.Activity.performResume(Activity.java:6791) 在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3477) 在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3546) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2795) 在android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1527) at android.os.Handler.dispatchMessage(Handler.java:110) at android .os.Looper.loop(Looper.java:203) at android.app.ActivityThread.main(ActivityThread.java:6251) 在java.lang.reflect。Method.invoke(Native Method) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1073) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 09-30 18:59:31.804 11339-11347/inducesmile.com.androidflashlightapp I/art:輸入while循環。

+0

請應用程式end Logcat –

+0

編輯問題時有格式化工具可用。沒有正確的格式化,很難閱讀logcat。 – jdv

+0

其中有許多潛在的重複:https://stackoverflow.com/q/26305107/1531971這些類似的問題對你來說有何不同? – jdv

回答

0

幾個月前,我也創建了基於手電筒傳感器的應用程序。我已經創建了flashlight活動代碼(包括java和xml),它似乎工作正常。看看下面的鏈接,看看是否有幫助:

https://gist.github.com/robillo/b27d37be3262164ee7f5532230c28c5a

https://gist.github.com/robillo/71afef65923138ed9d6011e3bd216249

另外,儘量做活動的處理部分中,如果塊中的onCreate(),如:

askForPermissions(); 
if(checkForPermission()){ 
    //Do your processing here 
} 

的功能有:

void askForPermissions(){ 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if(getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){ 
      getActivity().requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE); 
     } 
    } 
} 

boolean checkForPermission(){ 
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; 
} 
0

很難說沒有看到你的turnOnTheFlash和turnOffTheFlash功能,但我猜你沒有正確地釋放相機,如documentation所示。

+0

嘗試過很多,但沒有使用 –