Crop and Save Image to Device Specified Folder TheArtOfDev UdayAndroid

package com.uday.easyimagecropper; import android.app.ProgressDialog; import android.content.Context; import android.content.ContextWrapper; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.theartofdev.edmodo.cropper.CropImage; import com.theartofdev.edmodo.cropper.CropImageView; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; public class MainActivity extends AppCompatActivity { Button captureBtn,saveBtn,passportBtn; ImageView capturedImage; Uri finalUri; private ProgressDialog progressDialog; Bitmap bitmapp ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); captureBtn = (Button) findViewById(R.id.capture_btn); saveBtn = (Button) findViewById(R.id.save); passportBtn = (Button) findViewById(R.id.passport_photo); capturedImage = (ImageView) findViewById(R.id.captured_image); isStoragePermissionGranted(); isReadStoragePermissionGranted(); captureBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("TAG", "onClick: "); CropImage.activity() .setGuidelines(CropImageView.Guidelines.ON) .start(MainActivity.this); } }); saveBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { storeImage(bitmapp); //saveImageToInternalStorage(MainActivity.this,bitmapp); } }); passportBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } public boolean isStoragePermissionGranted() { if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { Log.v("TAG", "Permission is granted"); return true; } else { Log.v("TAG", "Permission is revoked"); ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); return false; } } else { //permission is automatically granted on sdk<23 upon installation Log.v("TAG", "Permission is granted"); return true; } } public boolean isReadStoragePermissionGranted() { if (Build.VERSION.SDK_INT >= 23) { if (checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) { Log.v("TAG", "Permission is granted1"); return true; } else { Log.v("TAG", "Permission is revoked1"); ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, 3); return false; } } else { //permission is automatically granted on sdk<23 upon installation Log.v("TAG", "Permission is granted1"); return true; } } @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){ CropImage.ActivityResult result = CropImage.getActivityResult(data); if(resultCode == RESULT_OK){ progressDialog =new ProgressDialog(this); progressDialog.setTitle("Uploading Image"); progressDialog.setMessage("Please wait for a while"); progressDialog.setCanceledOnTouchOutside(false); Uri resultantUri = result.getUri() ; finalUri = resultantUri; try { bitmapp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultantUri); } catch (IOException e) { e.printStackTrace(); } File thumb_filePath = new File(resultantUri.getPath()); try{ Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultantUri); capturedImage.setImageURI(resultantUri); } catch (Exception e){ e.printStackTrace(); } } } } private void storeImage(Bitmap image) { File pictureFile = getOutputMediaFile(); if (pictureFile == null) { Log.d("TAG", "Error creating media file, check storage permissions: ");// e.getMessage()); return; } try { FileOutputStream fos = new FileOutputStream(pictureFile); image.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.close(); } catch (FileNotFoundException e) { Log.d("TAG", "File not found: " + e.getMessage()); } catch (IOException e) { Log.d("TAG", "Error accessing file: " + e.getMessage()); } } private File getOutputMediaFile(){ // To be safe, you should check that the SDCard is mounted // using Environment.getExternalStorageState() before doing this. File mediaStorageDir = new File(Environment.getExternalStorageDirectory() + "/Android/data/" + getApplicationContext().getPackageName() + "/Files"); // This location works best if you want the created images to be shared // between applications and persist after your app has been uninstalled. // Create the storage directory if it does not exist if (! mediaStorageDir.exists()){ if (! mediaStorageDir.mkdirs()){ return null; } } // Create a media file name String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date()); File mediaFile; String mImageName="EI_"+ timeStamp +".jpg"; mediaFile = new File(mediaStorageDir.getPath() + File.separator + mImageName); Log.i("TAG", "getOutputMediaFile: Save image as "+mediaFile); return mediaFile; } public static Uri saveImageToInternalStorage(Context mContext, Bitmap bitmap){ String mTimeStamp = new SimpleDateFormat("ddMMyyyy_HHmm").format(new Date()); String mImageName = "snap_"+mTimeStamp+".jpg"; ContextWrapper wrapper = new ContextWrapper(mContext); File file = wrapper.getDir("Images",MODE_PRIVATE); file = new File(file, "snap_"+ mImageName+".jpg"); try{ OutputStream stream = null; stream = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,stream); stream.flush(); stream.close(); }catch (IOException e) { e.printStackTrace(); } Uri mImageUri = Uri.parse(file.getAbsolutePath()); Log.i("TAG", "getOutputMediaFile: Save image as "+mImageUri); return mImageUri; } }
To pick and crop image and save that image into specified folder with specified name;
This includes TheArtOfDev lib to pick and crop images.
This includes two permissions "Store and Read Storage Permission" .

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.