Crop intent issue with Android M

841 Views Asked by At

This is my existing code for the crop intent. This works fine on all devices

   private void choosePhotoFromCamera() {
System.out.println("choosePhotoFromCamera");
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (getExternalFilesDir(null) == null) {
        Log.e(TAG, "No SDcard");
        Log.e(TAG,
                "Maybe this will work : "
                        + Environment.getExternalStorageDirectory());
        Toast.makeText(ChooseFromCamera.this, "The app needs SDcard inserted",
                Toast.LENGTH_LONG).show();
        return;
    }
    File imagFile = null;
    try {
        imagFile = File.createTempFile("" + System.currentTimeMillis(),
                ".jpg", getExternalFilesDir(null));
         c= imagFile.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
    }

    uriForTempCapturePhoto = Uri.fromFile(imagFile);
    String uristring=   getPath(this, uriForTempCapturePhoto);
    uriForTempCapturePhoto= Uri.parse(uristring);*/
    i.putExtra(MediaStore.EXTRA_OUTPUT, uriForTempCapturePhoto);
    startActivityForResult(i, TAKE_PHOTO_REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    System.out.println("onActivityResult3333333333333333333333");
    if (requestCode == TAKE_PHOTO_REQUEST_CODE
            || requestCode == SELECT_PHOTO_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            Uri uri = null;
            if (requestCode == SELECT_PHOTO_REQUEST_CODE) {
                uri = intent.getData();
            } else if (requestCode == TAKE_PHOTO_REQUEST_CODE) {
                uri = uriForTempCapturePhoto;
            }
            if (uri != null) {
                    try {
                    u = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), c, null, null));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{
                }
                final Intent intent1 = new Intent(
                        "com.android.camera.action.CROP");
                    System.out.println(" uri of the image "+uriForTempCapturePhoto);

                intent1.setDataAndType(u, "image/*");
                intent1.putExtra("" +"", "true");
                intent1.putExtra("aspectX", 1);
                intent1.putExtra("aspectY", 1);
                intent1.putExtra("outputX", 100);
                intent1.putExtra("outputY", 100);
                intent1.putExtra("return-data", true);
                startActivityForResult(intent1, CUT_PHOTO_REQUEST_CODE);

            }
        }else{
            this.finish();
        }
    } else if (requestCode == CUT_PHOTO_REQUEST_CODE
            || requestCode == TAKE_GALLERY_REQUEST_CODE) {
        if (resultCode == RESULT_OK && intent != null) {
            System.out.println("4444444444444444444444444444444");
            Bitmap bm = intent.getParcelableExtra("data");
            if(bm != null){
                System.out.println("Test : " + bm.toString());
            }
            else
                System.out.println("Null Value");
            Bitmap bmpCircular = createCircularBitmap(bm);
            System.out.println("bmpCircular1111111111111111111"+bmpCircular);
            /*if (bmpCircular == null) {
                return;
            }
            String name = null;
            String path = Utils.saveBitmap(
                    ChooseFromCamera.this.getApplicationContext(), name,
                    bmpCircular);
            if (ChooseFromCamera.this == null) {
                Log.e(TAG, "called this at wrong time.");
                return;
            }
            if (path != null) {
                // sucessfully
                System.out.println("choosePhotoFromGallery1111111111111111111"+path);
                setContentView(R.layout.main);
                ivCamera = (ImageView) findViewById(R.id.iv_camera);
                ivAccount = (RoundCornerImageView) findViewById(R.id.iv_account_indicator);
                ivAccount.setImageBitmap(bmpCircular);
                ivCamera.setVisibility(View.INVISIBLE);
            }*/

            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
            bmpCircular.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
            byte[] byteArray = byteArrayOutputStream .toByteArray();
            String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
            //String encoded = "abcdef"; 
            System.out.println("encoded value"+encoded);

            ChooseCamera.callKonyFunction(encoded);
            this.finish();
        }else{
            this.finish();
        }

    } 
}

Now when I test it in android M, I am getting the following exception.

java.lang.IllegalArgumentException: mediaStoreUri must be a MediaStore Uri

To fix this issue insert image with mediastore and try to use the uri as below but this code also fails only in android M. Please help to fix.

if (uri != null) {
    try {
        u = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), c, null, null));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally{}

intent1.setDataAndType(u, "image/*");

Now i get only issue with Android M devices . The crop intent returns null in this case

0

There are 0 best solutions below