Bimap圖像不在服務器上。在數據庫映像表中有一個blob數據類型屬性。我想保存位圖圖像數據庫Android位圖圖像上傳到mysql數據庫作爲blob數據類型
class savebitmap extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
// Save the image to the SD card.
//File file = new File(Environment.getExternalStorageDirectory(),
//System.currentTimeMillis() + ".png");
//FileOutputStream stream = new FileOutputStream(file);
//bitmap.compress(CompressFormat.PNG, 100, stream);
//convert to byte
ByteArrayOutputStream bytedata = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bytedata);
byte[] data = bytedata.toByteArray();
String imagedata = Base64.encodeToString(data, Base64.DEFAULT);
String name="prescription";
//save image to mysql
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/android/image.php");
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("name",name));
nameValuePairs.add(new BasicNameValuePair("image",imagedata));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
Log.e("Connection", "connection success ");
Log.e("bitmap", imagedata);
} catch (Exception e) {
Log.e("upload failed", e.toString());
}
return null;
}
}
我的PHP文件,該文件從http請求recive的數據,並插入到數據庫
<?php
mysql_connect("localhost","root","") or die (mysql_error());
mysql_select_db("image") or die (mysql_errno());
$base= $_REQUEST['image'];
$name= $_REQUEST['name'];
$buffer = base64_decode($base);
$buffer = mysql_real_escape_string($buffer);
$flag['code']=0;
if($q=mysql_query("INSERT INTO image ('name','image')
VALUES ('$name',$buffer')"))
{
$flag['code']=1;
}
print(json_encode($flag));
mysql_close();
mysql_close();
?>
Logcat
09-12 19:07:43.486:D/dalvikvm(816):GC_FOR_ALLOC釋放< 1K,5%空閒6131K/6388K,暫停44ms,總計44ms 09-12 19:07:43.666:D/gralloc_goldfish(816) :未檢測到GPU仿真的仿真器。 09-12 19:07:46.946:D/dalvikvm(816):GC_FOR_ALLOC釋放2507K,43%空閒3651K/6388K,暫停68ms,總計69ms 09-12 19:07:46.946:I/dalvikvm-heap(816 ):爲1334416字節的分配增加堆(碎片情況)至4.974MB 09-12 19:07:46.986:D/dalvikvm(816):GC_CONCURRENT釋放1K,23%空閒4952K/6388K,暫停7ms + 3ms,總計37ms 09-12 19:07:46.986:D/dalvikvm(816):WAIT_FOR_CONCURRENT_GC被阻止17ms 09-12 19:07:49.766:D/dalvikvm(816):GC_CONCURRENT已釋放577K,20%空閒5124K/6388K,暫停4ms的+ 7毫秒,總44ms 09-12 19:07:50.946:E /連接(816):連接成功
您忘記了提問的部分。這段代碼是否以某種方式失敗?你有錯誤嗎?意外的行爲?你在問什麼? (另外,請注意,你的代碼中有一個***開放性的***注入漏洞***,你的SQL語句中也有一個語法錯誤,我確信數據庫告訴你一個錯誤消息,你也嘗試關閉數據庫兩次,這可能會導致另一個錯誤。) – David 2014-09-12 18:55:16
親愛的mybitmap圖像轉換成字節數組不上載在服務器端? – 2014-09-12 18:58:56
在建立連接時沒有錯誤 – 2014-09-12 18:59:55