0

I want to pick a picture by camera which is automatically compressed oder converted into a format which is easier to upload. After that I want to upload the image automatically.

Hope you can help me.

This is my code:

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mStorage = FirebaseStorage.getInstance().getReference();
    mProgressDialog1 = new ProgressDialog(this);
    mCamera = (ImageButton) findViewById(R.id.UpCamera);
    mImageview = (ImageView) findViewById(R.id.imageView1);


    mCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent1, CAMERA_REQUEST_CODE);
        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);



    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK){
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        mImageview.setImageBitmap(photo);
        Toast.makeText(MainActivity.this, "Uploading..", Toast.LENGTH_LONG).show();
        mProgressDialog1.setMessage("Crop Image");
        mProgressDialog1.show();
        Uri uri1 = data.getData();
        StorageReference filepath = mStorage.child("Tries").child(uri1.getLastPathSegment());
        filepath.putFile(uri1).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                mProgressDialog1.dismiss();
                Uri downloadUri = taskSnapshot.getDownloadUrl();
                Picasso.with(MainActivity.this).load(downloadUri).fit().centerCrop().into(mImageview);
                Toast.makeText(MainActivity.this, "Upload done!", Toast.LENGTH_LONG).show();

            }
        });

    }
}


}
Jessef
  • 57
  • 1
  • 10
  • Look [here](https://firebase.google.com/docs/storage/android/upload-files) in Firebase reference there is an example for how to upload a bitmap. In the example there is a Bitmap.compress call in which you can specify the compress quality. – Nicolas May 03 '17 at 21:20
  • encode your file to in 64base before upload it : [http://stackoverflow.com/questions/36189503/take-picture-and-convert-to-base64](http://stackoverflow.com/questions/36189503/take-picture-and-convert-to-base64) – Ibrahim Haouari May 03 '17 at 21:25
  • @IbrahimHaouari base64 encoding will make the data *bigger*, not smaller as Jessef is asking for. – Frank van Puffelen May 04 '17 at 03:34

1 Answers1

0

You need to add this methode then :

Bitmap bmp = BitmapFactory.decodeFile(miFoto)
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 70, bos);
InputStream in = new ByteArrayInputStream(bos.toByteArray());
ContentBody foto = new InputStreamBody(in, "image/jpeg", "filename");

like in :Compress camera image before upload

Community
  • 1
  • 1
Ibrahim Haouari
  • 240
  • 3
  • 11
  • Bitmap manipulation seems to be very heavy handed... prone to outofmemory error(OME). Wish android had something better to handle photos. This code prone to OME, if file is 40Mb decoding it to Bitmap will carsh your VM. – JPM Aug 18 '17 at 16:02
  • yes it's true I've seen this crash, can you give us the best solution for that please – Ibrahim Haouari Aug 23 '17 at 22:55
  • I would use glide to skip the step of setting up bitmap and then setting up contentbody. see this it may help, https://github.com/bumptech/glide/issues/1192 – JPM Sep 18 '17 at 17:25