crop the image and then send to server.Its working well,
add dependency in to gradle,
compile 'com.soundcloud.android:android-crop:1.0.1@aar'
gallery Intent,
private static final int PICK_FROM_FILE = 3;
private Uri mImageCaptureUri=null;
File sdCard = Environment.getExternalStorageDirectory();
String pathName;
File filepath;
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_FROM_FILE);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
// METHOD FOR CROP
beginCrop(mImageCaptureUri);
System.out.println("CHECK_GALLERY_path :" + mImageCaptureUri);
break;
case Crop.REQUEST_CROP:
handleCrop(resultCode, data);
break;
}
}
private void beginCrop(Uri source) {
Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
Crop.of(source, destination).start(this);
}
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
mImageCaptureUri = Crop.getOutput(result);
pathName = mImageCaptureUri.getPath();
//uploadImageToServer();
// getImageUri(mImageCaptureUri);
Picasso.with(Activity_My_Account.this).load(mImageCaptureUri).into(getimage); //IMAGE SAVE TO STORAGE
getImageUri(mImageCaptureUri);
System.out.println("path_name+++" + mImageCaptureUri);
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void getImageUri(Uri mImageCaptureUri) {
Bitmap photo = null;
try {
InputStream image_stream = getContentResolver().openInputStream(mImageCaptureUri);
java.util.Date date = new java.util.Date();
pathName = sdCard + "/" + "LEUF";
File myDir = new File(pathName);
System.out.println("GET_CHECK_PATH_NAME :" + pathName);
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
// if (file.exists()) file.delete();
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Bitmap bitmapOrg = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(image_stream), 350, 350, false);
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
filepath=file;
try {
// file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
//here call to upload image to server
public void UploadProfilePic(filepath){
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}