Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cropping image issue when taken from camera

Below is my related code snippet where in it redirects to the device's cropping app after the picture has been shot from the camera. I usually followed this example. But during the phase/moment between the picture taken from camera and making that available in crop app, it shows continuously loading and makes my app terribly unresponsive. If this happens at least once, every time I open the app, it just shows loading.

This issue doesn't occur in one scenario--> When I don't move my device(i.e., only when no screen orientation happens). Can some one please suggest me to avoid this issue?

public void shotFromCamera(View view)
{
    Intent intent    = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri);

                try {
                    intent.putExtra("return-data", true);

                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }
}

protected void onActivityResult(int requestCode, int resultCode,Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode==RESULT_OK && requestCode == PICK_FROM_CAMERA){

            doCrop();
                 } else if(resultCode == RESULT_OK && requestCode == CROP_FROM_CAMERA)
        {
            Bundle extras = data.getExtras();

            if (extras != null) {               
                Bitmap photoBitmap = extras.getParcelable("data");

                imageView.setImageBitmap(photoBitmap);
                if(mImageCaptureUri != null)
                {
                File f = new File(mImageCaptureUri.getPath());
                if (f.exists()) f.delete();
                }

                mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+ "/MyApp",
                           "avatar" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
                File file = new File(mImageCaptureUri.getPath()); 

                    selectedImagePath = mImageCaptureUri.getPath();
                    Log.d("pic1 ","pic to be created: "+selectedImagePath);
                    OutputStream fOut = null;

                    try {
                        fOut = new FileOutputStream(file);
                        photoBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                        fOut.flush();
                        fOut.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch(IOException e)
                    {
                        e.printStackTrace();
                    }       

            }


        }
}

    private void doCrop() {
            final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setType("image/*");

            List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

            int size = list.size();

            if (size == 0) {            
                Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

                return;
            } else {
                intent.setData(imageCaptureUri);

                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("scale", true);
                intent.putExtra("return-data", true);

                if (size == 1) {
                    Intent i        = new Intent(intent);
                    ResolveInfo res = list.get(0);

                    i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                    startActivityForResult(i, CROP_FROM_CAMERA);
                }
   }

To clarify my issue, I had run the source code in that example as it is, the same issue happens. I had run other couple of examples which redirect to the cropping app. The same issue happens while changing the orientation during the above explained phase.

P.S : If anyone has working example which surely works even on orientation changes, I would be glad to accept it as an answer.

like image 824
Kanth Avatar asked Jul 30 '13 06:07

Kanth


Video Answer


1 Answers

You can go with this-

  1. get the Bitmap from specified URI, i.e a full sized Image using

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(imageCaptureUri));

  2. Use Bitmap.CompressFormat.PNG while compressing the Image

  3. create a scaled bitmap using

    photo= Bitmap.createScaledBitmap(background, YourWidth, YourHeight, true);

  4. This is your Scaled (Cropped) Image.

like image 98
Mr.India Avatar answered Oct 08 '22 14:10

Mr.India