Question Detail

I have local Mobile Image file path and I am able to show that Image In my ImageView in my Android Application. I want to Crop or Rotate Image and Make one more Edited image in my Local directory and access that image in my Android Application Again.

I have given permission 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
File file = new File(mcoContext.getFilesDir(),getString(R.string.app_name));

How can Save my Cropped or Rotated Image in my Mobile App Folder in android?


Thread Reply

Hemant Sharma

- 9 months ago

Add it in your Gradle
compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'
Button editimg = (Button) findViewById(R.id.editImage_button_ID);
editimg.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Uri uri = Uri.fromFile(new File(imgEditpath));
        System.out.println(uri);
        Intent intent = CropImage.activity(uri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .getIntent(thiss);
        startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
    }
});

 

 

void setImgMethod(){
    if (imgEditpath  != null) {
        Glide.with(thiss).load(imgEditpath  + "").error(R.drawable.ic_wait_crash).into(editImage_ImageView_editImage);
    }
 }

 

@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();
            
wrtieFileOnStorage(getBaseContext(),resultUri.getHost(),resultUri.getPath());
            imgEditpath=resultUri.getPath();
            setImgMethod();
            imagesPath[imgposition]=resultUri.getPath();
            System.out.println(resultUri.getPath() + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
            isEditIMG = true;
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
            System.out.println(error + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        }
    }
}

 

public void wrtieFileOnStorage(Context mcoContext,String fileName, String editimg_path){
    File file = new File(mcoContext.getFilesDir(),getString(R.string.app_name));
    if(!file.exists()){
        file.mkdir();
    }
    System.out.println(file.getPath() + "^^^^^^^^^^^^^^^^LocalFILE^^^^^^^^^^^^^^^^^^^^^^^"+file.getName());
    try{
        File editfile = new File(file, fileName);
        FileWriter writer = new FileWriter(editfile);

        System.out.println(editfile.getPath() + "^^^^^^^^^^^^^^^^LocalFILE^^^^^^^^^^^^^^^^^^^^^^^");
        writer.append(editimg_path);
        writer.flush();
        writer.close();
    }catch (Exception e){
        e.printStackTrace();
        System.out.println(e.toString() + "^^^^^^^^^^^^^^^^LocalFILE^^^^^^^^^^^^^^^^^^^^^^^");

    }
}

Hemant Sharma

- 9 months ago

Add it in your Gradle
compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'
Button editimg = (Button) findViewById(R.id.editImage_button_ID);
editimg.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Uri uri = Uri.fromFile(new File(imgEditpath));
        System.out.println(uri);
        Intent intent = CropImage.activity(uri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .getIntent(thiss);
        startActivityForResult(intent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
    }
});

 

 

void setImgMethod(){
    if (imgEditpath  != null) {
        Glide.with(thiss).load(imgEditpath  + "").error(R.drawable.ic_wait_crash).into(editImage_ImageView_editImage);
    }
 }

 

@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();
            
wrtieFileOnStorage(getBaseContext(),resultUri.getHost(),resultUri.getPath());
            imgEditpath=resultUri.getPath();
            setImgMethod();
            imagesPath[imgposition]=resultUri.getPath();
            System.out.println(resultUri.getPath() + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
            isEditIMG = true;
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
            System.out.println(error + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        }
    }
}

 

public void wrtieFileOnStorage(Context mcoContext,String fileName, String editimg_path){
    File file = new File(mcoContext.getFilesDir(),getString(R.string.app_name));
    if(!file.exists()){
        file.mkdir();
    }
    System.out.println(file.getPath() + "^^^^^^^^^^^^^^^^LocalFILE^^^^^^^^^^^^^^^^^^^^^^^"+file.getName());
    try{
        File editfile = new File(file, fileName);
        FileWriter writer = new FileWriter(editfile);

        System.out.println(editfile.getPath() + "^^^^^^^^^^^^^^^^LocalFILE^^^^^^^^^^^^^^^^^^^^^^^");
        writer.append(editimg_path);
        writer.flush();
        writer.close();
    }catch (Exception e){
        e.printStackTrace();
        System.out.println(e.toString() + "^^^^^^^^^^^^^^^^LocalFILE^^^^^^^^^^^^^^^^^^^^^^^");

    }
}

Raj

- 9 months ago

  buttonCamera.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
// call android default camera
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 0);
                intent.putExtra("aspectY", 0);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 150);

                try {

                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, PICK_FROM_CAMERA);

                } catch (ActivityNotFoundException e) {
// Do nothing for now
                }
            }
        });

alex levine

- 9 months ago

Try this for RuntimePermission:

private boolean mayRequestContacts() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
        return true;
    }
    if (checkSelfPermission(WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        return true;
    }
    if (shouldShowRequestPermissionRationale(WRITE_EXTERNAL_STORAGE)) {
        Snackbar.make(mEmailView, R.string.permission_rationale, Snackbar.LENGTH_INDEFINITE)
                .setAction(android.R.string.ok, new View.OnClickListener() {
                    @Override
                    @TargetApi(Build.VERSION_CODES.M)
                    public void onClick(View v) {
                        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
                    }
                });
    } else {
        requestPermissions(new String[]{WRITE_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE);
    }
    return false;
}

/**
 * Callback received when a permissions request has been completed.
 */
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == REQUEST_READ_CONTACTS) {
        if (grantResults.length == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            populateAutoComplete();
        }
    }
}