0

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();

            }

        });
wawanopoulos
  • 9,614
  • 31
  • 111
  • 166
  • Why you are not uploading new bitmap that you are creating for preview image view? Check [the answer here](http://stackoverflow.com/questions/18573774/how-to-reduce-an-image-file-size-before-uploading-to-a-server) – Pr38y Jun 11 '14 at 18:50
  • @PreetiPatwa If i well understand the code in the post that you reference, decodeFile returns the adress of the new image to upload? In fact the first one has been overwriten? – wawanopoulos Jun 11 '14 at 18:58
  • No not overwritten. Its creating a new bitmap saving it to temp file and uploading that temp file. – Pr38y Jun 11 '14 at 19:07
  • I don't know how to pass as parameter for DESIREWIDTH and DESIREHEIGHT : `imagePath = decodeFile(imagePath, bmp.getWidth(), bmp.getHeight());` ? – wawanopoulos Jun 11 '14 at 19:08
  • [refer](http://stackoverflow.com/questions/9752628/image-size-for-android-app). desiredWidth*desiredHeight*4 ==2mb. Compute the current width/height ratio and set desiredWidth and desiredHeight accordingly. – Pr38y Jun 11 '14 at 19:24
  • how can you overwrite the image if the filename is timestamp based? –  Jun 13 '14 at 14:18

0 Answers0