繁体   English   中英

将图像上传到Android服务器,无需使用Volley编码到Base64

[英]Upload an image to server in Android without encoding to Base64 using Volley

我在android中有一个上传器,它上传了一个图像文件(由算法加密到400bytes)但文件必须被解码为base64字符串,以字符串形式保存到mysql数据库并将图像上传到文件夹。

问题是在将文件解码为base64字符串后,它会丢失其字节加密算法格式。(图像大小从400字节减少到更少)

这就是我所做的:

Bitmap b = this.toGrayscale(mRegisterImage);  //this image file is 400byte encrypted
uploadImage(b);

这是函数uploadimage

public String getStringImage(Bitmap bmp) {  //This is what converts the image to 64encoded format string
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);  //Thos is what compresses image hence loosing bytes
    byte[] imageBytes = baos.toByteArray();

    String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
    return encodedImage;
}

private void uploadImage(final Bitmap bmp) {
    // Showing the progress dialog
    try {
        final ProgressDialog loading = ProgressDialog.show(this,
                "Uploading fingerprint...", "Please wait...", false, false);
        StringRequest stringRequest = new StringRequest(
                Request.Method.POST, Config.url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        // Disimissing the progress dialog
                            debugMessage("Return Message: " + s);
                            loading.dismiss();
                                                }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        try { // Dismissing the progress dialog
                            loading.dismiss();
                            // Showing toast
                            debugMessage(volleyError.getMessage());
                        } catch (Exception r) {
                            debugMessage("onerrorresponse: "+volleyError.getMessage());
                        }
                    }
                }) {
            @Override
            protected Map<String, String> getParams()
                    throws AuthFailureError {
                // Converting Bitmap to String
                String image = getStringImage(bmp);

                // Creating parameters
                Map<String, String> params = new Hashtable<String, String>();

                // Adding parameters
                params.put("image", image);

                return params;
            }
        };
        // Creating a Request Queue
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        // Adding request to the queue
        requestQueue.add(stringRequest);
    } catch (Exception r) {
        debugMessage("out: "+r.getMessage());
    }
}

这是PHP代码:

  $sql ="SELECT id FROM fingerprint ORDER BY id ASC";

  $res = mysqli_query($con,$sql);

  $id = 0;

  while($row = mysqli_fetch_array($res)){
      $id = $row['id'];
  }

 $path = "$id.png";

 $actualpath = "http://simplifiedcoding.16mb.com/PhotoUploadWithText/$path";

 $sql = "INSERT INTO fingerprint (value) VALUES ('$actualpath')";

 if(mysqli_query($con,$sql)){

     file_put_contents($path,base64_decode($image));

     echo "Successfully Uploaded";
   }

一切都很好但有一种方法,我可以上传实际图像,而无需使用排球库将其转换为Android中的字符串。 上面的代码将图像压缩为字符串,如何更改此压缩以使字节不丢失

基本上没有,因为Http消息体只能携带byte data所以不能通过http协议发送任何其他形式的这种大数据。

Http通过使用TCP传输数据,在这种情况下使用通常的switching techniques 协议在形式的数据bitbyte ,其中平byte级主要用于所以如果你在寻找另一种方式,那么你的运气今天

因此,即使您使用StringRequest并将数据作为字符串parameter传递,最终您的每个parameter都将转换为byte array类型,因此您无法避免将数据转换为字节。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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