donglang6656 2015-09-25 09:31
浏览 142

图像从Android到MySQL Blob

Good day everybody! As the title say, I try to load an Image from an ImageView to MySQL. I want to save it like a Blob in MySQL.

I have seen several questions about this but no answers in my case: I still remain stuck!

Following is my 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"/>

Following is my JAVA activity

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

In the same activity, when I want to upload the picture, I put it in a 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));

Then I call an AsyncTask which call a php file itself with the contentValues as parameter. In the php file I get the "image" data and I try to load it in the MySQL "image" row of a table "image_personne", which has been define as "BLOB". I do it like this:

    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));
            }

There are no errors out, but that doesn't load! Could you help me to debug? Thank you!


This, is what is done now:

Android side:

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 side:

$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();
  • 写回答

2条回答 默认 最新

  • douyue8364 2015-09-25 09:40
    关注

    This will work,save it as byte[] array not as 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);
    
    评论

报告相同问题?

悬赏问题

  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致