简体   繁体   中英

How to compress image taken by camera before uploading to firebase?

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

            }
        });

    }
}


}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM