来自Camera的resultCode始终为0

时间:2017-07-16 14:38:26

标签: android android-intent android-camera-intent

结果摄像头的结果始终为0。

相机调用代码: -

Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                fileUri = getOutputMediaFileUri(Constants.ATTACH_IMAGE);
                imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
                imageIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());

                if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {

                    if (imageIntent.resolveActivity(getContext().getPackageManager()) != null) {
                        startActivityForResult(imageIntent, 1);
                    }
                    imageIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }
                else {
                    List<ResolveInfo> resInfoList = getContext().getPackageManager().queryIntentActivities(imageIntent, PackageManager.MATCH_DEFAULT_ONLY);
                    for (ResolveInfo resolveInfo : resInfoList) {
                        String packageName = resolveInfo.activityInfo.packageName;
                        getContext().grantUriPermission(packageName, fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    }

                    startActivityForResult(imageIntent, Constants.ATTACH_IMAGE);

                }

fileUri看起来像: - 内容://com.dummyname1.dummname2.provider/external_files/DummyName/IMG_SR7896f7f4-8eb7-47bc-990c-d46ad04c2a7c.jpeg

OnActivityResult: -

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Constants.ATTACH_IMAGE:

                if(resultCode ==Constants.RESULT_OK){
                    attachmentType="IMAGE";
                    attachment.setEnabled(false);
                    attachLayout.setVisibility(View.GONE);
                    Toast.makeText(getActivity().getApplicationContext(), R.string.successfull_image, Toast.LENGTH_SHORT).show();
                    File actualImage = null;
                    try {
                        actualImage = FileUtil.from(getContext(),fileUri);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if(actualImage!=null){

                        File compressedImage = new Compressor.Builder(getContext())
                                .setMaxWidth(1920).setMaxHeight(1440)
                                .setQuality(75)
                                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                                .setDestinationDirectoryPath(Constants.SavePath).build().compressToFile(actualImage);

                    }
                    add_Comments_on_Media_add("Photo added!");

                }
                else
                    mediaName="";
                break;

1 个答案:

答案 0 :(得分:-2)

您使用2个函数:startActivityForResult(),其中包含2个请求代码,API级别为1&gt; = 21; Constants.ATTACH_IMAGE代表API级别&lt; 21

但问题是你只处理case Constants.ATTACH_IMAGE:

试试这个:case Constants.ATTACH_IMAGE: case 1: