-1

I have task to capture image from camera and send that image to crop Intent. following is the code i have written

for camera capture

Intent captureIntent = new Intent(
                        MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);

In on Activity result

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

    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_CAPTURE) {
            // get the Uri for the captured image
            picUri = data.getData();  // picUri is global string variable 
            performCrop();
        }
   }

}

public void performCrop() {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");

        cropIntent.setDataAndType(picUri, "image/*");

        cropIntent.putExtra("crop", "true");

        cropIntent.putExtra("aspectX", 3);
        cropIntent.putExtra("aspectY", 2);
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);

        cropIntent.putExtra("return-data", true);

        startActivityForResult(cropIntent, CROP_PIC);
    } catch (ActivityNotFoundException anfe) {

        String errorMessage = "Your device doesn't support the crop action";
        Toast toast = Toast.makeText(getApplicationContext(), errorMessage,
                Toast.LENGTH_SHORT);
        toast.show();
    }
}

I am getting different behaviours on different devices

In some devices i am getting error "couldn't find item". In some devices after capturing image activity stuck and doesn't go ahead

I have also tried this

Please tell me the Right way to do this

Community
  • 1
  • 1
Nikhil
  • 3,711
  • 8
  • 32
  • 43

3 Answers3

1

//Declare this in class

private static int RESULT_LOAD_IMAGE = 1;

String picturePath="";

// write in button click event

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

startActivityForResult(i, RESULT_LOAD_IMAGE);

//copy this code in after onCreate()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         picturePath = cursor.getString(columnIndex);
        cursor.close();
        ImageView imageView = (ImageView) findViewById(R.id.img); //place imageview in your xml file
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
}

write the following permission in manifest file

1.read external storage

2.write external storage

Sagar Vasoya
  • 158
  • 1
  • 12
1

You can by calling the intent like below:

int REQUEST_IMAGE_CAPTURE = 1;
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                ((Activity) context).startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);

And in your activity inside OnActivityResult you get the path like this:

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_IMAGE_CAPTURE) {

                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);

                    Matrix matrix = new Matrix();

                    matrix.postRotate(-90);

                    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
                    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                    rotatedBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
                    byte[] attachmentBytes = byteArrayOutputStream.toByteArray();

                    String attachmentData = Base64.encodeToString(attachmentBytes, Base64.DEFAULT);


                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "CTSTemp" + File.separator + "default";
                    f.delete();
                    ESLogging.debug("Bytes size = " + attachmentBytes.length);
                    ESLogging.debug("FilePath = " + path);
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        ESLogging.error("FileNotFoundException while uploading new attachment in class HomeActivity", e);
                        e.printStackTrace();
                    } catch (IOException e) {
                        ESLogging.error("IOException while uploading new attachment in class HomeActivity", e);
                        e.printStackTrace();
                    } catch (Exception e) {
                        ESLogging.error("Exception while uploading new attachment in class HomeActivity", e);
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    ESLogging.error("Exception while uploading new attachment in class HomeActivity", e);
                    e.printStackTrace();
                }
            } 
}
    }
Hawraa Khalil
  • 261
  • 2
  • 12
0

try this tutorial http://www.androidhive.info/2013/09/android-working-with-camera-api/ this will help you

kiran kumar
  • 1,402
  • 17
  • 34