从base64字符串中将图像从android发送到PHP是一个好习惯吗? [英] Is it a good practice to send images in base64 string from android to PHP?

查看:82
本文介绍了从base64字符串中将图像从android发送到PHP是一个好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Android应用程序中我捕获图像或从库中选择图像。
然后我用base64字符串编码该图像。
然后我以JSON格式或普通邮件请求将该图像发送给PHP。
然后我将base64字符串解码为PNG图像并保存在服务器上。
然后将该图像的路径保存到mySQL image_url列。

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.

这是我现在正在工作的方式..它创建图像文件..但有时它创建损坏的图像文件(全黑或0 KB或20 KB大小)。而毕加索不适用于此类文件。它显示毕加索的解码器失败错误。

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.

这种方式将图像质量降低1.5 MB图像(在设备上)到50 KB图像(在服务器上)。我的编码为base64的代码是,

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.

欢迎编辑。

推荐答案

这是一个很好的做法如果你将图像保存为数据库中的字符串,但是因为你将图像转换回png并只保存mysql中的路径(这是一个非常好的做法),所以有意义转换为base64。

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.

我已经构建了一个Web应用程序以及显示/存储多个图像的移动应用程序。我建议,不要对它进行编码,只是以块的形式发送图像,你的php代码将组合这些块,创建一个图像文件并将图像的文件路径存储在数据库中(image_url)。

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).

这篇关于从base64字符串中将图像从android发送到PHP是一个好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆