【问题标题】:Upload an image to server in Android without encoding to Base64 using Volley使用 Volley 将图像上传到 Android 中的服务器而不编码到 Base64
【发布时间】:2016-09-14 16:36:03
【问题描述】:

我在 android 中有一个上传器,它上传一个图像文件(由算法加密到 400 字节),但该文件必须解码为 base64 字符串才能作为字符串保存到 mysql db 并将图像上传到文件夹。

问题是在将文件解码为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";
   }

一切正常,但有没有一种方法可以上传实际图像,而无需使用 volley 库将其转换为 android 中的字符串。上面的代码将图像压缩为字符串,如何更改此压缩方式以使字节不丢失

【问题讨论】:

  • 挑剔:base64 不是加密。这是一个编码。
  • 好的,但是文件大小和原始输出改变了
  • 加上你的数据库代码基本上完全被破坏了。您确实 NOT 通过提取所有现有 id 并丢弃除最后一个之外的所有 id 来获得一个 id。另外,您最终得到的 $id 不会是您在插入该 url 时创建的新记录的 id,因此您通过将所有内容都设置为 off-by-one 来终止 id/url 关联。
  • 原文件有加密
  • 主要问题不在于数据库,而在于将文件以原始格式发送到 php

标签: java php android mysql encryption


【解决方案1】:

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

Http 使用TCP 传输数据,在这种情况下通常使用switching techniques。协议在bitbyte 级别中格式化数据,其中byte 级别最常用,所以如果你在寻找另一种方式,那么你今天运气不好

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

【讨论】:

  • 上面传输的是一个字符串,原始文件是一个字节的形式,如何传输这个字节而不转换成字符串格式
  • 让我在我的回答中添加更多信息
  • 如果我错了,请纠正我,谁曾否决过这个问题,否则如果可以的话,请给出答案,与我们分享您的知识
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-11
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
相关资源
最近更新 更多