对于摄像机活动,onActivityResult()中的结果代码为-1。如何进行调试?

问题描述 投票:0回答:2

[这里所有提交内容都没有否定结果代码。我知道这意味着任务失败,但是我不知道任务如何或为什么失败。该应用程序会打开相机,然后我就可以拍照。

btnN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                captureImage(v);
            }
        });

功能定义如下。

public void captureImage(View view) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (cameraIntent.resolveActivity(getPackageManager()) != null) {
            File imageFile = null;
            try {
                imageFile = getImageFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (imageFile != null) {
                Uri imageUri = FileProvider.getUriForFile(this, "com.example.testingproject.fileprovider", imageFile);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                startActivityForResult(cameraIntent, 1);
            }
            System.out.println("imageFile length:" + imageFile.length());

        }

    }

在上述功能中,我什至尝试发送请求代码为2。相同的功能,我可以单击图片,但存在相同的问题。

    public File getImageFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmsss").format(new Date());
        String imageName = "jpg_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);

        File imageFile = File.createTempFile(imageName, ".jpg", storageDir);
        currentImagePath = imageFile.getAbsolutePath();
        System.out.println("currImPath: " + currentImagePath);
        return imageFile;
    }

下面急需的onActivityResult()代码

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        try {
            System.out.println("reqCode: "+requestCode+" resCode: "+resultCode);
            switch (requestCode) {
                case 0: {
                    if (resultCode == RESULT_OK) {
                        File file = new File(currentImagePath);
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), Uri.fromFile(file));
                        if (bitmap != null) {
                        //...Do your stuffs
                            Toast.makeText(MainActivity.this, "Bitmap NOT null", Toast.LENGTH_SHORT).show();
                            imgView.setImageBitmap(bitmap);
                        }
                        else
                        {
                            Toast.makeText(MainActivity.this,"BitmapNull",Toast.LENGTH_SHORT).show();
                        }
                    }




                    break;
                }
                default: Toast.makeText(MainActivity.this,"result code not okay",Toast.LENGTH_SHORT).show(); break;
            }

        } catch (Exception error) {
            error.printStackTrace();
        }
    }

日志中的sysout在下面

2020-05-01 13:52:24.644 10014-10014/com.example.testingproject I/System.out: reqCode: 1 resCode: -1
android android-studio android-camera
2个回答
0
投票

-1等于RESULT_OK。这意味着任务已成功完成


0
投票

当您查看代码时。 -1RESULT_OK的有效状态代码>

 public static final int RESULT_OK = -1;

现在您已经在此行中将requestCode设置为1

startActivityForResult(cameraIntent, 1);

所以在您的OnActivityResult中显示

reqCode: 1 resCode: -1

是正确的。

© www.soinside.com 2019 - 2024. All rights reserved.