在上传到firebase之前如何压缩相机拍摄的图像?

时间:2017-05-03 21:10:23

标签: android image upload firebase-storage

我想通过相机选择一张图片,该图片会自动压缩或转换为更易于上传的格式。 之后我想自动上传图片。

希望你能帮助我。

这是我的代码:

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

            }
        });

    }
}


}

1 个答案:

答案 0 :(得分:0)

您需要添加此方法:

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

喜欢在:Compress camera image before upload