我的图像没有被压缩-我该怎么办?

时间:2019-02-19 12:42:57

标签: java android image compression

我想将图像发送到具有压缩大小的服务器,所以我在这里检查代码,并在其中放置方法的地方创建了getStringImage(Bitmap bmp),因为我是android开发人员中的新手,请帮助我

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {


    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            imagefilepath = resultUri.getPath();
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), resultUri);
                uploading = getStringImage(bitmap);
                imageView.setVisibility(View.VISIBLE);
                upload.setVisibility(View.GONE);
                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
            Exception error = result.getError();
            Toast.makeText(PurchaseBillActivity.this,error.getMessage(),Toast.LENGTH_LONG).show();
        }
    }
}

public String getStringImage(Bitmap bmp){
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG,50, bos);
    byte[] imageBytes = bos.toByteArray();
    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

0 个答案:

没有答案