Question Detail

I am Cropping Image and save New Cropped Image in my SDCard Mobile Location, Image is going to save there but That image is not Readable what I want when I read that Image is Black.

Why not saving Cropped Image?
but if I read cropped Image from Temp file it is fine but not saving in the folder as it is where I am missing anything? Please Suggest me the right way to Crop Image and Save In the Path location in Android Mobile. 

Below is my code what I am doing :

@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();
             
            String filename = Pref.getString(thiss, "userName_pref").replace(" ", "_") + resultUri.getPath().substring(resultUri.getPath().lastIndexOf("/") + 1).replace("cropped", "");
            System.out.println(resultUri.getPath() + "^^^^^^^^^^^^^^^^^CROPED IMG^^^^^^^^^^^^^^^^^^^^^^" + filename);
            imgEditpath = resultUri.getPath();
            wrtieFileOnStorage(filename, 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(String sFileName, String path) {
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + getString(R.string.app_name) + "/");
    if (!file.exists()) {
        file.mkdir();
    }
    System.out.println(file.getPath() + "^^^^^^^^^^^^^^^^Cache LocalFILE^^^^^^^^^^^^^^^^^^^^^^^" + file.getName());
    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        File gpxfile = new File(file, sFileName);
        file.createNewFile();
        FileOutputStream writer = new FileOutputStream(gpxfile);
        System.out.println(gpxfile.getPath() + "^^^^^^^^^^^^^^^^Local FILE^^^^^^^^^^^^^^^^^^^^^^^" + gpxfile.getAbsolutePath());
        setImgMethod();
        writer.write(bytes.toByteArray());
        // writer.append(path);
        writer.flush();
        writer.close();

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println(e.toString() + "^^^^^^^^^^^^^^^^LocalFILE^^^^^^^^^^^^^^^^^^^^^^^");

    }
}


Thread Reply

Nick Johnson

- 1 years ago

You have to convert Your URI into Bitmap Follow me :

@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) {
            try {
                Uri resultUri = result.getUri();
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), resultUri);
                imgEditpath =  saveToInternalStorage(bitmap);
                System.out.println(imgEditpath + "^^^^^^^^^^^^^^^^^CROPED IMG^imgEditpath^^^^^^^^^^^^^^^^^^^^^");

            } catch (Exception e) {
                System.out.println(e + "^^^^^^^^^^^^^^^^^CROPED IMG^EXCEPTION^^^^^^^^^^^^^^^^^^^^^");
            }
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
            System.out.println(error + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
        }
    }
}

Anonymous

- 4 months ago

Below code will convert Image into Base64 Code.
Convert Image to String encoding

String picturePath = IMAGE_PATH_HERE;
String encodedImage = Base64.encodeToString(convertImageToBase(picturePath), Base64.DEFAULT);
Log.d("PICK_IMAGE_BASE", encodedImage);

 

if you want to decode your base64 encoded String to Image

byte[] imageByteArray = Base64.decode(encodedImage, Base64.DEFAULT);