无法从base64字符串获得完整的图像质量

时间:2015-10-23 11:03:56

标签: java android mysql bitmap

** 我可以使用这段代码将图像保存在远程Mysql数据库中。但是我无法从这段代码中获得完整的图像质量。图像的质量非常差。有人建议使用它的解决方案吗?

提前致谢。 **

public void onActivityResult(int reqCode, int resCode, Intent data) {

        if (resCode == RESULT_OK) {
            if (reqCode == 1) {
                Bitmap photo = null;
                imageURI = data.getData();
                try {



      photo = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageURI);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Image.setImageBitmap(photo);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                photo.compress(Bitmap.CompressFormat.JPEG, 90, stream); //compress to which format you want.
                byte[] byte_arr = stream.toByteArray();
                String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
                try {
                    image_str1 = URLEncoder.encode(image_str, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

        }
        if (reqCode == CAMERA_REQUEST && resCode == RESULT_OK) {

            Bitmap photo = (Bitmap) data.getExtras().get("data");
            Image.setImageBitmap(photo);
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 90, stream); //compress to which format you want.
            byte[] byte_arr = stream.toByteArray();
            String image_str = Base64.encodeToString(byte_arr, Base64.DEFAULT);
            try {
                image_str1 = URLEncoder.encode(image_str, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            //  Photo=getBytes(photo);
        }

    }

2 个答案:

答案 0 :(得分:2)

尝试以下代码:

sklearn

<强> encodeImagetoString()

if (requestCode == SELECT_FILE && resultCode == RESULT_OK
                    && null != data) {


                Uri selectedImage = data.getData();
                String[] filePathColumn = { MediaStore.Images.Media.DATA };


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

                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                imgPath = cursor.getString(columnIndex);
                cursor.close();
}else  if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK ) {  

                imgPath = fileUri.getPath();
                if (imgPath != null && !imgPath.isEmpty()) {                            

                encodeImagetoString();

                } else {
                Toast.makeText(getApplicationContext(),                                     getResources().getString(R.string.some_error_occured),
                                    Toast.LENGTH_LONG).show();
                        }
}

希望它会对你有所帮助。

答案 1 :(得分:2)

而不是使用

photo.compress(Bitmap.CompressFormat.JPEG, 90, stream);

尝试使用

photo.compress(Bitmap.CompressFormat.JPEG, 100, stream);