【发布时间】: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