我目前正在使用一个Android应用程序来处理图片,并将其上传到数据库。
该应用程序首先使用设备的摄像头,然后将生成的位图发送到AsyncTask,该任务将调用PHP API以将图像作为mediumBLOB和附加数据(int和String)更新数据库。
AsyncTask将位图编码为字符串,并将所有需要的数据作为POST请求发送到PHP页面。
然后PHP页面收集数据,将字符串转换为BLOB图像,并在mysqli请求中使用它们在数据库表中插入新行。
更新时间:
感谢您的反馈。我更改了PHP代码,以使用绑定变量。
尽管如此,当我将图像设置为参数时,请求会生成一个带有空BLOB的新表行。
我知道通常的方法是将图像存储在其他地方,并在数据库中使用它们的名称/url;但在我的例子中,使用blob是一项要求。
以下是AsyncTask的Android代码:
public class DBTaskImageSending extends AsyncTask<String, Void, Void>
{
static final String TAG = "asyncSend";
private final String PARAMETER_PLACE = "location";
private final String PARAMETER_IMAGE = "image";
private final String PARAMETER_COMMENT = "comment";
private final String PARAMETER_OWNER = "owner";
Context mCContext;
UploadActivity mUAMain;
String mURLAdress;
String mComment;
int mID_Photographer;
int mID_Event;
String mImage;
public DBTaskImageSending(String url, UploadActivity ua, Context c, Bitmap img, String comment, int place, int owner)
{
this.mURLAdress = url;
this.mCContext = c;
this.mUAMain = ua;
this.mComment = comment;
this.mID_Event = place;
this.mID_Photographer = owner;
this.mImage = conv_Image_String(img);
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
Log.i(TAG, "Request sent to "+mURLAdress);
}
@Override
protected void onPostExecute(Void result)
{
Log.i(TAG,"__________________________________________________");
super.onPostExecute(result);
}
@Override
protected Void doInBackground(String... strings)
{
try
{
sendImage(mURLAdress);
Log.i(TAG, "Object sent !");
}
catch (IOException ioe)
{
Log.i(TAG, "Sending failed.");
}
return null;
}
public void sendImage(String url) throws IOException
{
String mSCryptedData = URLEncoder.encode(PARAMETER_PLACE, "UTF-8")
+ "=" + URLEncoder.encode(Integer.toString(mID_Event), "UTF-8");
mSCryptedData += "&" + URLEncoder.encode(PARAMETER_OWNER, "UTF-8")
+ "=" + URLEncoder.encode(Integer.toString(mID_Photographer), "UTF-8");
mSCryptedData += "&" + URLEncoder.encode(PARAMETER_COMMENT, "UTF-8")
+ "=" + URLEncoder.encode(mComment, "UTF-8");
mSCryptedData += "&" + URLEncoder.encode(PARAMETER_IMAGE, "UTF-8")
+ "=" + URLEncoder.encode(mImage,"UTF-8");
ContentValues mCV = new ContentValues();
mCV.put(PARAMETER_PLACE,mID_Event);
mCV.put(PARAMETER_OWNER,mID_Photographer);
mCV.put(PARAMETER_COMMENT,mComment);
mCV.put(PARAMETER_IMAGE,mImage);
try {
final HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(5000);
conn.setConnectTimeout(5000);
conn.setDoOutput(true);
OutputStreamWriter mOSW = new OutputStreamWriter(conn.getOutputStream());
mOSW.write(mSDataCryptee);
mOSW.flush();
readResponse(conn.getInputStream());
}
catch (Exception e)
{
Log.i(TAG,e.getCause().toString());
}
}
private void readResponse(InputStream pISBuilder) throws IOException
{
Log.i(TAG,"readR : Entering...");
BufferedReader mBR = new BufferedReader(new InputStreamReader(pISBuilder));
String ligne;
Log.i(TAG,"readR : Starting to read");
while ((ligne = mBR.readLine()) != null)
{
Log.i(TAG,ligne);
}
}
private String conv_Image_String(Bitmap imgOrigin)
{
if (imgOrigin != null)
{
ByteArrayOutputStream mBOS = new ByteArrayOutputStream();
imgOrigin.compress(Bitmap.CompressFormat.PNG,10, mBOS);
byte[] mBytImg = mBOS.toByteArray();
String mStrImg = Base64.encodeToString(mBytImg, Base64.DEFAULT);
return mStrImg;
}
else
{
return null;
}
}
}
我重新编写了PHP代码,下面是新版本:
<?php
if ((isset($_POST['location']) AND isset($_POST['owner'])) AND (isset($_POST['comment']) AND isset($_POST['image']))) {
$location = $_POST['location'];
$comment = $_POST['comment'];
$owner = $_POST['owner'];
$image = base64_decode($_POST['image']);
$image_b = imagecreatefromstring($image);
if ($image_b == false) {
echo "error_image";
}
else {
$image_temp = imagegd($image_b, 'tmp');
$image_f = fopen('tmp',"r");
$image_blob = fread($image_f,filesize('tmp'));
sendData ($location,$comment,$owner,$image_blob);
}
}
else {
echo "error_parameters";
}
function sendData($p_location,$p_comment,$p_owner,$p_image){
$conn = mysqli_connect("localhost", "****", "****", "**DBName**", "3306");
if (!$conn) {
die('Could not connect to MySQL: ' . mysqli_connect_error());
}
else {
mysqli_query($conn, 'SET NAMES \'utf8\'');
$sqli = "INSERT INTO bar_a_image_test.Photo(
ID_Localisation_Photo,
ID_Photographe,
Date_Photo,
Ecartee,
Commentaire_Photo,
Image_Photo)
VALUES
( ? , ? , NULL, 0, ?, ? )";
if ($stmt = $conn->prepare($sqli));
{
$stmt->bind_param("iisb",$p_location, $p_owner, $p_comment, $p_image);
$stmt->execute();
}
$conn->close();
}
}
?>
现在的新问题是,查询加载数据库中的所有数据,除了图像。它只在新行中记录一个空BLOB。
我正在寻找一种有效地将图像(编码为字符串)转换为BLOB的方法。
正如我之前提到的,我必须使用blob,尽管外部存储更加频繁。