简体   繁体   English

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

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

I have an uploader in android which uploads an image file(which is encrypted by an algorithim to 400bytes) but the file has to be decoded to base64 string to be saved to a mysql db as a string and upload the image to a folder. 我在android中有一个上传器,它上传了一个图像文件(由算法加密到400bytes)但文件必须被解码为base64字符串,以字符串形式保存到mysql数据库并将图像上传到文件夹。

The problem is that after decoding the file to base64 string it looses its byte encrypted algorithm format.(The image size reduces from 400 bytes to less) 问题是在将文件解码为base64字符串后,它会丢失其字节加密算法格式。(图像大小从400字节减少到更少)

This is what i have done: 这就是我所做的:

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

This is the function uploadimage 这是函数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());
    }
}

This is the PHP CODE: 这是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";
   }

Everything works quite fine but is there a way that i can upload the actual image without converting it to a string in android using volley library. 一切都很好但有一种方法,我可以上传实际图像,而无需使用排球库将其转换为Android中的字符串。 The above code compresses the image to the string, How do i change this compression so that the bytes arent lost 上面的代码将图像压缩为字符串,如何更改此压缩以使字节不丢失

Basically no, because Http message body can carry byte data only so can't send any other form of this large data through http protocol. 基本上没有,因为Http消息体只能携带byte data所以不能通过http协议发送任何其他形式的这种大数据。

Http transfer data by using TCP which use commonly switching techniques in this case. Http通过使用TCP传输数据,在这种情况下使用通常的switching techniques Protocols either format data in bit or byte level where byte level is mostly used so if you looking for another way then you are out of luck today 协议在形式的数据bitbyte ,其中平byte级主要用于所以如果你在寻找另一种方式,那么你的运气今天

So even if you are using StringRequest and passing data as string parameter eventually your every parameter is converted to byte array type so you can't escape from converting data to bytes. 因此,即使您使用StringRequest并将数据作为字符串parameter传递,最终您的每个parameter都将转换为byte array类型,因此您无法避免将数据转换为字节。

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

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