我想從Android上的圖像上傳到我的SQL數據庫,我有這樣的代碼:JSON和上傳圖像到服務器
private void uploadFile() {
// TODO Auto-generated method stub
Bitmap bitmapOrg= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() +"/Chart1.png");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList nameValuePairs = new
ArrayList();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://ipadress/base.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
}
,但在同一時間,我要上傳我的用戶名太我的數據庫(假設我使用edittext檢索用戶名),任何人都知道如何做到這一點?我應該添加什麼樣的代碼?由於之前
我的數據庫表應該是這樣的:
ID |用戶名|文件|
,並且我可以用它來上傳字符串數據的JSON代碼是這樣的:
private void uploadFile() {
// TODO Auto-generated method stub
String nama = getIntent().getStringExtra("user");
Bitmap bitmapOrg= BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getAbsolutePath() +"/Chart1.png");
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
ArrayList nameValuePairs = new ArrayList();
nameValuePairs.add(new BasicNameValuePair("image",ba1));
nameValuePairs.add(new BasicNameValuePair("username",nama));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://139.195.144.67/BloodGlucose/base2.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse httpRespose = httpclient.execute(httppost);
HttpEntity httpEntity = httpRespose.getEntity();
InputStream in = httpEntity.getContent();
BufferedReader read = new BufferedReader(new InputStreamReader(in));
String isi= "";
String baris= "";
while((baris = read.readLine())!=null){
isi+= baris;
}
//Jika isi tidak sama dengan "null " maka akan tampil Toast "Register Success" sebaliknya akan tampil "Register Failure"
if(!isi.equals("null")){
Toast.makeText(this, "Register Success", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Register Failure", Toast.LENGTH_LONG).show();
}
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
我可以將這些代碼?或者有另一種方式來從android同時上傳文件和字符串?由於之前
我的PHP代碼:
<?php
include_once("koneksi.php");
$username = $_REQUEST['username'];
$hasil = mysql_query("select (max(ID)+1)as newid from userownfile");
$row = mysql_fetch_row($hasil);
$base = $_REQUEST['image'];
$filename = $row[0] . ".jpg";
$buffer=base64_decode($base);
$path = "img/".$filename.".jpg";
$handle = fopen($path, 'wb');
$numbytes = fwrite($handle, $buffer);
fclose($handle);
$conn=mysql_connect("localhost","root","");
mysql_select_db("db_bloodglucose");
$sql = "insert into userownfile(username,file) values('$username','" . $path . "')";
mysql_query($sql);
$string= "select * from userownfile";
$my_string= mysql_query($string);
if($my_string){
while($object= mysql_fetch_assoc($my_string)){
$output[] = $object;
}
echo json_encode($output);
?>
我以前從未使用過這個,所以我不知道如何做到這一點,你能告訴我一個完整的例子嗎?在此之前感謝 – Handy
我編輯了答案,向您展示瞭如何使用帶有MultiPartEntity的HttpPost。從這裏獲取jar文件http://www.docjar.com/jar_detail/httpmime-4.0.1.jar.html – Bear
@Bear我很好奇Base64的圖像大小限制。爲什麼會有一個? –