2013-03-23 44 views
-3

我修改了人們提供的代碼,我面臨一個問題,它在Android中運行得非常好。但是..它沒有給出任何迴應,也沒有給出數據庫..行後功能仍然保持不變。我做了什麼錯誤?謝謝!圖片上傳器在數據庫中沒有響應mysql

Android的代碼放在這裏:

public class MainActivity extends Activity { 
     private ProgressDialog pDialog; 
    Uri currImageURI; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button gallery_btn = (Button)findViewById(R.id.gallerybtn); 
    gallery_btn.setOnClickListener(new OnClickListener(){ 
     public void onClick(View view){ 
      //to open up a gallery browser 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      startActivityForResult(Intent.createChooser(intent,"Select Picture.."),1);   

     } 

    }); 

    Button upload_btn = (Button)findViewById(R.id.uploadbtn); 
    upload_btn.setOnClickListener(new OnClickListener(){ 
     public void onClick(View view){ 

      new HttpUploader().execute(getRealPathFromURI(currImageURI));  

     } 

    }); 

} 

// To handle when an image is selected from the browser 
public void onActivityResult(int requestCode, int resultCode, Intent data){ 
    if (resultCode == RESULT_OK) { 
     if (requestCode == 1) { 
      // currImageURI is the global variable I’m using to hold the content: 
      currImageURI = data.getData(); 

      EditText tv_path = (EditText) findViewById(R.id.path); 
      tv_path.setText(getRealPathFromURI(currImageURI)); 

     } 

    } 
} 

//Convert the image URI to the direct file system path of the image file 
public String getRealPathFromURI(Uri contentUri) { 
    String [] proj={MediaStore.Images.Media.DATA}; 
    android.database.Cursor cursor = managedQuery(contentUri, 
      proj,  // Which columns to return 
      null,  // WHERE clause; which rows to return (all rows) 
      null,  // WHERE clause selection arguments (none) 
      null);  // Order-by clause (ascending by name) 
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
    cursor.moveToFirst(); 
    String c = cursor.getString(column_index); 
    return c; 
} 

public class HttpUploader extends AsyncTask<String, Void, String> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Uploading Image.. Please wait..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    protected String doInBackground(String... path) { 

     String outPut = null; 

     for (String sdPath : path) { 

      Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath); 
      ByteArrayOutputStream bao = new ByteArrayOutputStream(); 

      //Resize the image 
      double width = bitmapOrg.getWidth(); 
      double height = bitmapOrg.getHeight(); 
      double ratio = 400/width; 
      int newheight = (int)(ratio*height); 

      System.out.println("———-width" + width); 
      System.out.println("———-height" + height); 

      bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true); 

      //Here you can define .PNG as well 
      bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao); 
      byte[] ba = bao.toByteArray(); 
      String ba1=Base64.encodeBytes(ba); 

      String id = "99"; 
      ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair("image", ba1)); 
      nameValuePairs.add(new BasicNameValuePair("path", sdPath)); 
      nameValuePairs.add(new BasicNameValuePair("id", id)); 

      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://127.0.0.1/upload.php"); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity();     

       // print responce 
       outPut = EntityUtils.toString(entity); 
       Log.i("GET RESPONSE—-", outPut); 

       //is = entity.getContent(); 
       Log.e("log_tag ******", "good connection"); 

       bitmapOrg.recycle(); 

      } catch (Exception e) { 
       Log.e("log_tag ******", "Error in http connection " + e.toString()); 
      } 
     } 
     return outPut; 
    } 

    protected void onPostExecute(String file_url) { 
      // dismiss the dialog once got all details 
      pDialog.dismiss(); 
     } 
} 
} 

logcat中顯示了這種只有:log_tag ** 「」 良好的連接

PHP文件放在這裏:

<?php 
include "db_connection.php"; 

if (isset($base)) { 
    $base = $_REQUEST["image"]; 
    $filepath = $_REQUEST["path"]; 
    $id = $_REQUEST["id"]; 

    $image_name = "/image/".$filepath; 

    // base64 encoded utf-8 string 
    $binary = base64_decode($base); 

    // binary, utf-8 bytes 

    header("Content-Type: bitmap; charset=utf-8"); 

    $file = fopen($image_name, "wb"); 

    fwrite($file, $binary); 

    fclose($file); 

    $result = mysql_query("UPDATE restaurants SET image = '$image_name' WHERE ID = '$id'"); 

    if(mysql_affected_rows > 0){ 
     echo json_encode("success!"); 
    }else{ 
     echo json_encode("failed!"); 
    } 

} else { 
     echo json_encode("missing required attribute!"); 
} 

?>   

回答

3

替換此代碼部分:

$ result = mysql_query(「U PDATE餐廳SET image ='$ image_name'WHERE ID ='$ id'「);

if(mysql_affected_rows() <> 0){ 
    echo json_encode("success!"); 
}else{ 
    echo json_encode("failed!"); 
} 
+0

它仍然是相同的>。<但無論是成功/失敗我應該怎麼檢查? 我應該Log.e(「」,??);或在Android的東西? – 2013-03-23 11:18:08

+0

$ result = mysql_query(); ex – ankit 2013-03-23 12:13:47

+0

「UPDATE restaurants SET image ='$ image_name'WHERE ID ='$ id'」part put on mysql_query(); – ankit 2013-03-23 12:14:06