从位图中取出图像并将其放入字符串中

时间:2016-03-23 07:44:27

标签: android bitmap imageview

String bitmap1将从Bitmap photo获取压缩图像, 但问题在这里,我认为bitmap1imageview获取图像而不是photo,因为当我将图像发送到服务器时,它会发送像imageview一样的小分辨率,虽然我设置压缩100

    private void takeImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            selectedImage = data.getData();
            photo = (Bitmap) data.getExtras().get("data");

            String[] filepath = {MediaStore.Images.Media.DATA};

            Cursor cursor = getContentResolver().query(selectedImage, filepath, null , null , null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filepath[0]);
            picturePath = cursor.getColumnName(columnIndex);
            cursor.close();

            Bitmap photo = (Bitmap) data.getExtras().get("data");
            imageView = (ImageView) findViewById(R.id.tokenImage);
            imageView.setImageBitmap(photo);

        }
    }

....

ByteArrayOutputStream bao = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 100, bao);
            byte[] by = bao.toByteArray();
            bitmap1 = Base64.encodeToString(by, Base64.DEFAULT);

1 个答案:

答案 0 :(得分:1)

 Try this-   

 Bitmap resized = Bitmap.createScaledBitmap(photo , 600,370, true);
        ByteArrayOutputStream blob = new ByteArrayOutputStream();
        resized.compress(Bitmap.CompressFormat.JPEG, 100, blob);

       String StrBase64 = Base64.encodeToString(blob.toByteArray(), Base64.DEFAULT);