Here is my code which reproduce this scenario :
- The user click on "Take photo" button and take a picture
- The user click on "Create annonce" button, which upload the captured photo to the server.
All these parts works fine, but the problem that i would like to solve is that the size of uploaded image is so big : around 2Mo.
I would like to create a shadow copy of captured image for creating a smaller photo based on the first. (We can overwrite the first if it's possible).
I don't know how to do this, and where to do this in my code ?
Code to initialize destination file :
String uniqueID = UUID.randomUUID().toString();
String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
imageToStore = name + uniqueID + ".jpg";
destination = new File(Environment.getExternalStorageDirectory(), imageToStore);
When the user click on button, it start photo capture :
takePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
startActivityForResult(intent, REQUEST_IMAGE);
overridePendingTransition(R.anim.left_to_right, R.anim.right_to_left);
}
});
Code to treat the capture :
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
try {
preview.setVisibility(View.VISIBLE);
takePhoto.setVisibility(View.GONE);
txtHaut.setText("Cette image est parfaite !");
FileInputStream in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
imagePath = destination.getAbsolutePath();
Log.d("INFO", "PATH === " +imagePath);
Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
preview.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else{
tvPath.setText("Request cancelled");
}
}
And finally, when the user click on "Create annonce" button, it upload the photo :
btnCreate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(New_annonce_act_step3.this, "", "Création de votre annonce en cours ..", true);
new Thread(new Runnable() {
public void run() {
uploadFile(imagePath);
}
}).start();
}
});