2012-06-28 128 views
0

我想將圖像文件上傳到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的),任何人都可以幫我?由於之前

問候

+0

$ _REQUEST [ '名' ]不是失敗的原因,但你有一個較低的cas在這裏。它應該是$ _REQUEST ['文件名'] :-) –

+1

也是你得到什麼錯誤?你確定你有一個數據庫連接?我也看不到你在哪裏執行你的SQL? –

+0

我試過了,但圖像仍然沒有顯示,我不知道我是否已經錯過了我的代碼,btw文件是在設備中,圖像庫(模擬器) –

回答

1

你$ sql中看起來錯誤,請嘗試:

$sql = "UPDATE login SET file = '$path' WHERE username_user = '$username'"; 
$r = mysql_query($sql); 
+1

也只是讓你知道mysql_函數現在全部被刪除http://www.php.net/manual/en/mysqlinfo.api.choosing.php –

+0

很高興知道:) –

+0

我已經改變了代碼,但是結果仍然是相同的,btw在MySQL我把文件作爲LONGBLOB類型,我不知道如果錯誤是在我的android代碼? –

0

此行

$sql = "UPDATE login SET file = '" . $path . "' Where username_user = $username; $r=mysql_query($sql); 

應該讀

$sql = "UPDATE login SET file = '" . $path . "' Where username_user = " . $username; $r=mysql_query($sql); 
+0

爲你做了這個工作? –

+0

它仍然沒有工作,嗯,我想知道我的android代碼中有什麼東西丟失? –