我想將圖像文件上傳到Android Mobile Programming的mysql數據庫中,但我遇到了一些問題。這裏是我的代碼:Android:將圖像上傳到mysql
我的基於Eclipse代碼:
private void uploadFile() {
// TODO Auto-generated method stub
String nama = getIntent().getStringExtra("user");
Bundle fieldresults = this.getIntent().getExtras();
Filepath = fieldresults.getString("bitmap");
Bitmap bitmapOrg= BitmapFactory.decodeFile(Filepath);
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("filename",Filepath));
nameValuePairs.add(new BasicNameValuePair("username_user",nama));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new
HttpPost("http://10.0.2.2/BloodGlucose/uploadImage.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());
}
}
和我的PHP代碼:
<?php
include_once("koneksi.php");
$username = $_REQUEST['username_user'];
$hasil = mysql_query("select * from login");
$base = $_REQUEST['image'];
$filename = $_ReQUEST['filename']
$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 = "UPDATE login SET file = '" . $path . "' Where username_user = $username;
$r=mysql_query($sql);
?>
的問題是文件我的數據庫中沒有顯示(MySQL的),任何人都可以幫我?由於之前
問候
$ _REQUEST [ '名' ]不是失敗的原因,但你有一個較低的cas在這裏。它應該是$ _REQUEST ['文件名'] :-) –
也是你得到什麼錯誤?你確定你有一個數據庫連接?我也看不到你在哪裏執行你的SQL? –
我試過了,但圖像仍然沒有顯示,我不知道我是否已經錯過了我的代碼,btw文件是在設備中,圖像庫(模擬器) –