【问题标题】:Image From Android to MySQL Blob从 Android 到 MySQL Blob 的图像
【发布时间】:2015-12-23 02:28:51
【问题描述】:

大家好!正如标题所说,我尝试将图像从 ImageView 加载到 MySQL。我想在 MySQL 中像 Blob 一样保存它。

我已经看到了几个关于此的问题,但在我的情况下没有答案:我仍然卡住了!

以下是我的 XML ImageView:

<ImageView
                    android:id="@+id/das_photoib"
                    android:layout_width="150dp"
                    android:layout_height="150dp"
                    android:maxWidth = "150dp"
                    android:maxHeight="150dp"
                    android:layout_gravity="center_horizontal"/>

以下是我的 JAVA 活动

rootView = inflater.inflate(R.layout.devenirassistant, container, false);
photoprofil = (ImageView) rootView.findViewById(R.id.das_photoib);

在同一个activity中,当我要上传图片的时候,我把它放在了一个ContentValues中:

ContentValues cv = new ContentValues();
Bitmap bitmap = ((BitmapDrawable)photoprofil.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
cv.put("image", Arrays.toString(b));

然后我调用一个 AsyncTask,它以 contentValues 作为参数调用一个 php 文件本身。 在 php 文件中,我得到“图像”数据,并尝试将其加载到表“image_personne”的 MySQL“图像”行中,该表已定义为“BLOB”。我是这样做的:

    if (isset($_POST['image'])){
                $image = imagecreatefromstring(base64_decode($_POST['image']));

                $request5 = "INSERT INTO image_personne (id_personne, isDeleted, image, isModere) VALUES ({$_POST['id']}, '0', $image, 0)";

$result5 = mysqli_query($connexion,$request5) or die(mysqli_error($connexion));
            }

没有错误,但没有加载!你能帮我调试一下吗?谢谢!


这就是现在要做的:

安卓端:

ContentValues cv = new ContentValues();
Bitmap bitmap = ((BitmapDrawable)photoprofil.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
cv.put("image", Base64.encode(b,Base64.DEFAULT));

php端:

$request5 = "INSERT INTO image_personne (id_personne, isDeleted, image, isModere) VALUES (?,?,?,?)";

            $image = $_POST['image'];
            $id = $_POST['id'];
            $del = '0';
            $mod = '0';
            $stmt = $connexion->prepare($request5); //prepare the statements
            $stmt->bind_param("isbi", $id, $del, $null, $mod ); //bind parameter
            $stmt->send_long_data(0, $image);   //add blob data to statement
            $stmt->execute();

【问题讨论】:

  • 现在,因为我更正了一个部分(参见另一个答案),它看起来像是在 php.ini 中。我不知道如何在 blob 中转换我的 byte[]

标签: php android mysql imageview blob


【解决方案1】:

这会起作用,将它保存为byte[]数组而不是Array.toString()

ContentValues cv = new ContentValues();
Bitmap bitmap = ((BitmapDrawable)photoprofil.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
cv.put("image", b);

【讨论】:

  • 感谢您的回答。我试过了,它在 php 文件上返回错误:警告:imagecreatefromstring():空字符串或无效图像。所以我想我必须删除 imagecreatefromstring 并且只写: $image = base64_decode($_POST['image']);但它返回另一个错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '?w?5, 0)' 附近使用正确的语法。我猜图像格式不好。你知道我要做什么吗?
【解决方案2】:

我会尽量回答你的 php 方面。这里我假设$_POST['image'] 作为base64_encode 编码格式的文件输入。所以我们将把它作为base64编码格式插入到数据库中作为blob。也会用

  1. 我们以 base64_encod 格式接收数据
  2. 将其作为 blob 保存在数据库中
  3. 检索数据并解码从字符串创建的图像

    if (isset($_POST['image']))  {    $request5 = "INSERT INTO image_personne (id_personne, isDeleted, image,sModere)
     VALUES (?, ?, ?,?)";  //statement for db update
        $image = $_POST['image'];     //assign image the received data
        $id = $_POST['id'];   //assign values to variables
        $isdel = '0';
        $ismod = 0;
        $stmt = $connexion->prepare($request5); //prepare the statements
        $stmt->bind_param("isbi", $id, $isdel, $null, $ismod ); //bind parameter
        $stmt->send_long_data(0, $image);   //add blob data to statement
        $stmt->execute();  //execute the statement
        }
    

更新:

   Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);

你可以试试这个方法进行图片文件到字符串的转换。

【讨论】:

  • 好的,我试过了,但是当我插入mysql时,图像现在是空的......你没有对此做错吗:“bind_param("isbi", $id, $isdel, $空,$ismod);"?不是 $image 而不是 $null 吗?谢谢
  • $stmt->send_long_data(0, $image);这将在执行期间将您的数据添加到数据库。顺便说一句,您可以显示数据发送代码/发送数据类型。 @水坝
  • 但是 php 怎么知道在 send_long_data 中将 $image 发送到哪里呢?实际上,它可以是第一个字段、第二个字段、第三个字段……或者它是通过您在 bind_param 中放置“$null”的位置检测到的?在 MySQL 中:id_personne 是 INT(11),isDeleted 是 TINYINT(1),image 是 BLOB,isModere 是 TINYINT(1)
  • 没有。在绑定参数中,b 用于 blob。发送长数据仅适用于 blob。无需担心此代码,在本地主机和公共服务器上测试。
  • 很奇怪,因为那行不通。它填充了所有其他字段,但将图像保留为 null :( 我不明白为什么!
猜你喜欢
  • 1970-01-01
  • 2017-08-16
  • 1970-01-01
  • 2013-03-05
  • 2019-06-30
  • 2014-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多