Android Question Crop native to b4a

Douglas Farias

Expert
Licensed User
Longtime User
Can someone make this code to b4a?

B4X:
public void takePhoto(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, PICK_FROM_CAMERA);
}

public void browsePhotos(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(intent, PICK_FROM_MEDIASTORE);
}

...

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && (requestCode == PICK_FROM_CAMERA || requestCode == PICK_FROM_MEDIASTORE)) {

        // creating the file I want to modify
        File dir = getDir("img", Context.MODE_PRIVATE);
        File f = new File("croppedImage.jpg");
        try {
            FileOutputStream fos = new FileOutputStream(f);
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(
                    this.getContentResolver(), data.getData());
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        } catch (FileNotFoundException e) {
            Log.e("FileNotFoundException", "Error while writing file in onActivityResult");
            e.printStackTrace();
        } catch (IOException e) {
            Log.e("IOException", "Error while editing bitmap");
            e.printStackTrace();
        }

        // returns lot of errors when I'm trying to do this
        // imageFileUri = Uri.fromFile(f);

        // works correctly but modify the original image
        imageFileUri = data.getData();

        Intent intent2 = new Intent("com.android.camera.action.CROP", imageFileUri);
        intent2.putExtra("crop", "true");
        intent2.putExtra("aspectX", 80);
        intent2.putExtra("aspectY", 107);
        intent2.putExtra("outputX", 640);
        intent2.putExtra("outputY", 856);
        intent2.putExtra("scale", true);
        intent2.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                imageFileUri);

        startActivityForResult(intent2, CROP_PHOTO);
    }

    else if (resultCode == RESULT_OK && requestCode == CROP_PHOTO) {
                    // method displaying the image from the Uri
        saveTakenPicture(imageFileUri);
    }
}

thx
 
Top