ImageFile Found Null in One Plus Device While Capture Image From Camera

240 views Asked by At

I'm trying to get an Image from the Camera and external storage. Then Send it to crop then save the image on the server. Other devices like Note 7 Pro and Samsung -G920F works fine with this code But Trouble found in OnePlus A600. Please Help me to solve this

 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);
                    }


                }
            });

When I try to get imageFile on StartActivity For Result I found imageFile null So, it not able to go on CropActivity.

0

There are 0 answers