ACTION_IMAGE_CAPTURE无法提供结果ResultInfo

时间:2014-08-13 11:12:11

标签: android android-activity camera

在SAMSUNG TAB 3中捕获图像时出现错误onActivityResult(),(对于Nexus Working):

08-13 16:18:17.101: E/AndroidRuntime(13801): FATAL EXCEPTION: main
08-13 16:18:17.101: E/AndroidRuntime(13801): java.lang.RuntimeException: Unable to resume activity {com.koops.test/com.salesman.activities.ExpenseAddNewActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1010, result=-1, data=null} to activity {com.koops.test/com.salesman.activities.ExpenseAddNewActivity}: java.lang.NullPointerException
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2658)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2686)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2159)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3595)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.access$800(ActivityThread.java:146)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.os.Looper.loop(Looper.java:137)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.main(ActivityThread.java:4949)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at java.lang.reflect.Method.invokeNative(Native Method)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at java.lang.reflect.Method.invoke(Method.java:511)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1043)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at dalvik.system.NativeStart.main(Native Method)
08-13 16:18:17.101: E/AndroidRuntime(13801): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1010, result=-1, data=null} to activity {com.koops.test/com.salesman.activities.ExpenseAddNewActivity}: java.lang.NullPointerException
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3224)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2645)
08-13 16:18:17.101: E/AndroidRuntime(13801):    ... 13 more
08-13 16:18:17.101: E/AndroidRuntime(13801): Caused by: java.lang.NullPointerException
08-13 16:18:17.101: E/AndroidRuntime(13801):    at com.salesman.activities.ExpenseAddNewActivity.onActivityResult(ExpenseAddNewActivity.java:467)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.Activity.dispatchActivityResult(Activity.java:5369)
08-13 16:18:17.101: E/AndroidRuntime(13801):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3220)
08-13 16:18:17.101: E/AndroidRuntime(13801):    ... 14 more

是否有适用于所有设备的通用代码。

    public void openImageIntent(View view) {

        // Determine Uri of camera image to save.
        final File root = new File(KOOPSv3.getFolderPath(context, context.getResources().getString(R.string.folder_expense)));

        final String fname = staff_id + new Date().getTime() + Math.abs(new Random().nextInt()) + ".jpg";

        path = root.getPath() + File.separator + fname;

        Log.d("koopsv3", "PATH : " + path);

        final File sdImageMainDirectory = new File(root, fname);
        outputFileUri = Uri.fromFile(sdImageMainDirectory);

        // Camera.
        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);

        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            cameraIntents.add(intent);
        }

        // File System.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_PICK);

        // Chooser of file system options.
        final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Image");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));

        startActivityForResult(chooserIntent, 1010);
    }

结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == RESULT_OK)
    {
        if(requestCode == 1010)
        {
            final boolean isCamera;
            if(data == null)
            {
                isCamera = true;
            }
            else
            {
                final String action = data.getAction();
                if(action == null)
                {
                    isCamera = false;
                }
                else
                {
                    isCamera = action.equals(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                }
            }

            Uri selectedImageUri;
            if(isCamera) {
                selectedImageUri = outputFileUri;

                Log.i("koopsv3", "OutputURI : "+path);
            } else {
                selectedImageUri = data == null ? null : data.getData();
                path = com.ipaulpro.afilechooser.utils.FileUtils.getPath(context, selectedImageUri);
            }
        }
    }
}

onActivityResult()我正在获得平板电脑3的NULL,我该如何解决这个问题。

1 个答案:

答案 0 :(得分:2)

  

我该如何解决这个问题

如果Intentnull,请检查您的文件是否在您请求的位置。如果你这样做,请使用它。如果您不这样做,请告诉用户他们选择了破解的应用程序,请再试一次。

您传递给Intent方法的onActivityResult()是您从其他应用程序收到的输入。您需要使用防御性编程方法,因为输入可能并不总是您期望的。从理论上讲,ACTION_IMAGE_CAPTURE会返回一致的结果。实际上,成千上万的相机应用程序有数千种不同的实现方式,并不是每个开发人员都会对其进行充分的测试 ACTION_IMAGE_CAPTURE处理。