2016-02-29 102 views
0

以下是我試圖直接打開Android Default Camera的代碼。它適用於大多數設備,如果安裝了其他相機應用程序,則可以在不創建選配器的情況下打開默認相機。Android相機問題(特定於設備)

private void callTheCamera(){ 

     String pkgName = ""; 
     PackageManager pm = getPackageManager(); 
     List<ApplicationInfo> installedApps = pm.getInstalledApplications(0); 

     for (ApplicationInfo ai: installedApps) { 

      if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) { 
       // System app - do something here 
       if(ai.toString().toLowerCase().contains("camera")){ 
        pkgName = ai.packageName; 
       } 
      } else { 
       // User installed app? 
      } 
     } 

     if(!pkgName.equals("")){ 
      try { 
       Intent LaunchIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       LaunchIntent.setPackage(pkgName); 
       LaunchIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); 
       startActivityForResult(LaunchIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
      } catch (Exception e) { 
       e.printStackTrace(); 
       Intent LaunchIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       LaunchIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); 
       startActivityForResult(LaunchIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
      } 
     } 
     else{ 
      Intent LaunchIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      LaunchIntent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri()); 
      startActivityForResult(LaunchIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
     } 
    } 

然而,應用程序崩潰的幾臺設備具有以下問題:

Fatal Exception: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE pkg=com.sonymobile.android.addoncamera.timeshift (has extras) } 
     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1638) 
     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1430) 
     at android.app.Activity.startActivityForResult(Activity.java:3457) 
     at android.app.Activity.startActivityForResult(Activity.java:3418) 
     at android.support.v4.app.FragmentActivity.startActivityForResult(SourceFile:784) 
     at com.goldvip.crownit.BasicCameraActivity.callTheCamera(SourceFile:674) 
     at com.goldvip.crownit.BasicCameraActivity.access$000(SourceFile:60) 
     at com.goldvip.crownit.BasicCameraActivity$2.run(SourceFile:136) 
     at android.os.Handler.handleCallback(Handler.java:808) 
     at android.os.Handler.dispatchMessage(Handler.java:103) 
     at android.os.Looper.loop(Looper.java:193) 
     at android.app.ActivityThread.main(ActivityThread.java:5398) 
     at java.lang.reflect.Method.invokeNative(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:940) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
     at dalvik.system.NativeStart.main(NativeStart.java) 

現在,如何有效地解決這個??。

+0

哪個操作系統版本? – Riser

+0

它分散在Android OS 4,5和... – asdec90

+0

什麼是setImageUri()。 ? – Riser

回答

0

好,異常似乎非常清楚對我說:

無活動處理意向{ 行爲= android.media.action.IMAGE_CAPTURE

在代碼中,你假設如果某個系統應用的軟件包名稱中包含camera,則該應用必須能夠處理該ACTION_IMAGE_CAPTURE意圖。但顯然(如例外告訴你的那樣)並非總是如此

您可能需要抓住ActivityNotFoundException並嘗試用戶安裝的應用程序,或者作爲最後的手段告訴用戶您無法拍照。你知道,有沒有相機的Android設備。

此外,我認爲有更好的方法來找到可以執行某些意圖的應用程序,而不是簡單地檢查它們的軟件包名稱。