0

我正在尝试从相机和外部存储中获取图像。然后将其发送到裁剪,然后将图像保存在服务器上。Note 7 Pro 和三星-G920F 等其他设备可以正常使用此代码,但在 OnePlus A600 中发现问题。请帮我解决这个问题

 public Intent getPickImageChooserIntent() {

            Uri outputFileUri = getCaptureImageOutputUri();
            Log.d(TAG, "getPickImageChooserIntent: outputFileUri : : " + outputFileUri);

            List<Intent> allIntents = new ArrayList<>();
            PackageManager packageManager = getPackageManager();

            Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
            for (ResolveInfo res : listCam) {
                Intent intent = new Intent(captureIntent);
                intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                intent.setPackage(res.activityInfo.packageName);
                if (outputFileUri != null) {
                    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputFileUri);
                }
                allIntents.add(intent);
            }

            Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
            galleryIntent.setType("image/*");
            List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
            for (ResolveInfo res : listGallery) {
                Intent intent = new Intent(galleryIntent);
                intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                intent.setPackage(res.activityInfo.packageName);
                allIntents.add(intent);
            }

            Intent mainIntent = allIntents.get(allIntents.size() - 1);
            for (Intent intent : allIntents) {
                if (intent.getComponent() != null &&
                        intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
                    mainIntent = intent;
                    break;
                }
            }
            allIntents.remove(mainIntent);

            Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));

            return chooserIntent;
        }
        /**
         * Get URI to image received from capture by camera.
         */
        private Uri getCaptureImageOutputUri() {
            Uri outputFileUri = null;
            File getImage = getApplicationContext().getExternalCacheDir();
            String fileName = "IMG_" + System.currentTimeMillis() + "_.jpg";
            // When you are using 24 or higher version then you need to set FileProvider to set path of your image file
            if (getImage != null) {
                imageFile = new File(getImage.getPath(), fileName);
                Log.d(TAG, "getCaptureImageOutputUri: imageFile : : " + imageFile);
                // outputFileUri = Uri.fromFile(imageFile);
                outputFileUri = FileProvider.getUriForFile(SiteInspectionActivity.this, getPackageName() + ".provider", imageFile);
            }
            return outputFileUri;
        }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (AppConstance.DEBUG)
            Log.d(TAG, "onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");

        if (AppCompatActivity.RESULT_OK != resultCode)
            return;

        boolean isCamera = true;

        if (data != null) {
            String action = data.getAction();
            isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
        }

        Log.e("TESTT", "onActivityResult isCamera: " + isCamera);

        Log.d(TAG, "onActivityResult: imageFile : : " + imageFile);

        switch (requestCode) {

            case REQ_CODE_CROP:

                uploadOnSelectedFragment();

                break;
            case REQUEST_CODE_PIC_IMAGE:

                Bitmap bm = null;
                if (!isCamera) {

                    if (data.getData() != null && imageFile != null) {

                        FileOutputStream fos = null;

                        try {
                            fos = new FileOutputStream(imageFile);
                            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
                            if (bm == null) {

                                Toast.makeText(SiteInspectionActivity.this, "Invalid file.", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            bm.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                        } catch (IOException e) {
                            e.printStackTrace();
                        } finally {
                            try {
                                if (fos != null) {
                                    fos.close();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }

                if (imageFile != null) {
                    Log.e("TESTT", "onActivityResult: " + imageFile.getAbsolutePath());
                    gotoImageCrop(imageFile.getAbsolutePath(), bm);
                }
                break;
        }

    }

    /**
         * Created to open image crop activity.
         *
         * @param imageFile
         */
        public void gotoImageCrop(String imageFile, Bitmap bm) {

            Intent intent = new Intent(SiteInspectionActivity.this, CropImageActivity.class);

            intent.putExtra("imageFile", imageFile);
            if (bm != null) {

                intent.putExtra("aspectWidth", bm.getHeight());
                intent.putExtra("aspectHeight", bm.getWidth());
            }

            startActivityForResult(intent, REQ_CODE_CROP);

        }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        Log.d(AppConstance.DEBUG_TAG, "onRequestPermissionsResult() called with: requestCode = [" + requestCode + "], permissions = [" + permissions + "], grantResults = [" + grantResults + "]");

        if (requestCode == REQUEST_PERMISSIONS_REQUEST_CODE) {

            boolean isPerpermissionForAllGranted = false;

            if (grantResults.length > 0 && permissions.length == grantResults.length) {

                for (int i = 0; i < permissions.length; i++) {

                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                        isPerpermissionForAllGranted = true;
                    } else {
                        isPerpermissionForAllGranted = false;
                    }
                }

            } else {
                isPerpermissionForAllGranted = true;
            }

            if (isPerpermissionForAllGranted) {
                startActivityForResult(getPickImageChooserIntent(), REQUEST_CODE_PIC_IMAGE);
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    }
// This is how I call StartActivityForResult 

    ivCamera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (checkPermissions()) {
                        startActivityForResult(getPickImageChooserIntent(), REQUEST_CODE_PIC_IMAGE);
                    }


                }
            });

当我尝试在 StartActivity 上获取 imageFile 时,我发现 imageFile null 所以,它无法继续 CropActivity。

4

0 回答 0