Android相机问题(特定于设备)

时间:2016-02-29 07:12:23

标签: android 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)

现在,如何有效地解决这个问题。??

1 个答案:

答案 0 :(得分:0)

嗯,异常似乎对我很清楚:

  

找不到处理Intent的Activity {   ACT = android.media.action.IMAGE_CAPTURE

在您的代码中,您认为如果某个系统应用程序的软件包名称中包含camera,则该应用程序必须能够处理ACTION_IMAGE_CAPTURE意图。但显然(正如Exception告诉你的那样)并非总是如此

在这种情况下,您可能需要抓住ActivityNotFoundException并尝试用户安装的应用,或作为最后的手段告诉用户您无法拍照。你知道,有没有相机的Android设备。

此外,我认为有更好的方法可以找到可以执行某些意图的应用,而不是简单地检查其包名。