ArthurHub / Android-Image-Cropper

Image Cropping Library for Android, optimized for Camera / Gallery.
Apache License 2.0
6.4k stars 1.38k forks source link

How do I save cropped? #667

Open BrianKiarieMwaniki opened 5 years ago

BrianKiarieMwaniki commented 5 years ago

How do I save cropped image after using this build pattern?

// start picker to get image for cropping and then use the image in cropping activity CropImage.activity() .setGuidelines(CropImageView.Guidelines.ON) .start(this);

SamuelHauptmannvanDam commented 4 years ago

Did you ever figure it out?

BrianKiarieMwaniki commented 4 years ago

Yes, I did figure it out.

Thanks for getting back to me.

On Sun, 15 Dec 2019, 09:57 AudaciousSam, notifications@github.com wrote:

Did you ever figure it out?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ArthurHub/Android-Image-Cropper/issues/667?email_source=notifications&email_token=AIWWNROAL2WORTBYXD3N5KDQYXIMZA5CNFSM4HEGLKL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEG4SXYA#issuecomment-565783520, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIWWNRKZ7FZYVPEMYNEZMYTQYXIMZANCNFSM4HEGLKLQ .

semnazri commented 4 years ago

hi can you explain how to save cropped image result?

BrianKiarieMwaniki commented 4 years ago

On the onActivityResult method I created a Uri which I used to create a bitmap

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if(resultCode == RESULT_OK){
            Uri resultUri = result.getUri();

            if(data!=null){
                Uri contentUri = data.getData();
                try{
                    Context myContext = MainActivity.getContextOfApplication();
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(myContext.getContentResolver(),resultUri);
                    saveTempBitmap(bitmap);

After I created the bitmap, I called a method saveTempBitmap(bitmap) and passed the bitmap to it, but I first check if the external storage is available for read and write:

//check if external storage is available for read and write
private boolean isExternalStorageWritable() {
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        return true;
    }
    return false;
}

Here is my saveTempBitmap(bitmap) method:

private void saveImage(Bitmap finalBitmap) {

    File myDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
            + File.separator + "AppName" + File.separator + "photos");
    myDir.mkdirs();

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String fname = userName + timeStamp + ".jpg";
    Log.i("Image path: ",fname);

    File file = new File(myDir,fname);
    if(file.exists()) file.delete();
    try{
        FileOutputStream out = new FileOutputStream(file);
        finalBitmap.compress(Bitmap.CompressFormat.JPEG,100,out);
        out.flush();
        out.close();
    }catch (Exception e){
        e.printStackTrace();
    }
}

I hope this is helpful.

semnazri commented 4 years ago

hi @BrianKiarieMwaniki ! thanks for sharring! it works!

BrianKiarieMwaniki commented 4 years ago

Your welcome.

On Tue, May 19, 2020 at 2:39 PM Sem Nazri notifications@github.com wrote:

hi @BrianKiarieMwaniki https://github.com/BrianKiarieMwaniki ! thanks for sharring! it works!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ArthurHub/Android-Image-Cropper/issues/667#issuecomment-630763018, or unsubscribe https://github.com/notifications/unsubscribe-auth/AIWWNRLIW554UUFE4VF6LFDRSJVVLANCNFSM4HEGLKLQ .