dongxiangshen7916 2016-02-19 16:41
浏览 63

从base64字符串中将图像从android发送到PHP是一个好习惯吗?

In my android application I Capture image or select image from gallery. then I encode that image in base64 string. then I send that image to PHP in JSON format or with normal post request. then I decode base64 string to PNG image and save on server. then save path of that image to mySQL image_url column.

This is the way I am currently working now..Which create image file.. but sometime it creates corrupt image file(full black or 0 KB or 20 KB in size). and Picasso does not work on such files. It shows decoder failed error of picasso.

This way reduces Image quality 1.5 MB image(on device) to 50 KB image (on server). my code for encoding to base64 is,

`   // method for bitmap to base64
    public static String encodeTobase64(Bitmap image) {
        Bitmap immage = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
        return imageEncoded;
    }`

If anyone have suggestions please help. or other way for sending and saving images.

EDITS are welcomed.

  • 写回答

1条回答 默认 最新

  • drau67562 2016-02-19 17:03
    关注

    It's a good practice "If" you are saving your images as strings in your database, but since you are converting the image back to png and saving just the path in mysql (which is a very good practice), doesn't make sense to convert to base64.

    I have built a web application as well as mobile apps which display/ store multiple images. I would suggest, don't encode it, just send images in chunks and your php code will combine those chunks, create an image file and store the file path of the image in the database(image_url).

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记