2016-12-04 108 views
0

現在我只能上傳文本到MySQL服務器。如何將圖像從sqlite路徑上傳到mysql服務器?

DBcontrooler.java // My SQLite。

public String composeJSONfromSQLite(){ 
    ArrayList<HashMap<String, String>> wordList; 
    wordList = new ArrayList<HashMap<String, String>>(); 
    String selectQuery = "SELECT * FROM home where udpateStatus = '"+"no"+"'"; 
    SQLiteDatabase database = this.getWritableDatabase(); 
    Cursor cursor = database.rawQuery(selectQuery, null); 
    if (cursor.moveToFirst()) { 
     do { 
      HashMap<String, String> map = new HashMap<String, String>(); 
      map.put("idhome", cursor.getString(0)); 
      map.put("mprefix", cursor.getString(1)); 
      map.put("name", cursor.getString(2)); 
      map.put("address", cursor.getString(3)); 
      map.put("district", cursor.getString(4)); 
      map.put("amphoe", cursor.getString(5)); 
      map.put("mprovince", cursor.getString(6)); 
      map.put("community", cursor.getString(7)); 
      map.put("lat", cursor.getString(8)); 
      map.put("longt", cursor.getString(9)); 
      map.put("user", cursor.getString(10)); 
      map.put("cb1", cursor.getString(11)); 
      map.put("cb2", cursor.getString(12)); 
      map.put("cb3", cursor.getString(13)); 
      map.put("cb4", cursor.getString(14)); 
      map.put("cb5", cursor.getString(15)); 
      map.put("cb6", cursor.getString(16)); 
      map.put("cb7", cursor.getString(17)); 
      map.put("cb8", cursor.getString(18)); 
      map.put("cb9", cursor.getString(19)); 
      map.put("cb10", cursor.getString(20)); 
      map.put("cb11", cursor.getString(21)); 
      map.put("cb12", cursor.getString(22)); 
      map.put("cb13", cursor.getString(23)); 
      map.put("cb14", cursor.getString(24)); 
      map.put("cb15", cursor.getString(25)); 
      map.put("cb16", cursor.getString(26)); 
      map.put("cb17", cursor.getString(27)); 
      map.put("cb18", cursor.getString(28)); 
      map.put("cb19", cursor.getString(29)); 
      map.put("cb20", cursor.getString(30)); 
      map.put("cb21", cursor.getString(31)); 
      map.put("cb22", cursor.getString(32)); 
      map.put("cb23", cursor.getString(33)); 
      map.put("cb24", cursor.getString(34)); 
      map.put("cb25", cursor.getString(35)); 
      map.put("cb26", cursor.getString(36)); 
      map.put("cb27", cursor.getString(37)); 
      map.put("cb28", cursor.getString(38)); 
      map.put("cb29", cursor.getString(39)); 
      map.put("cb30", cursor.getString(40)); 
      map.put("cb31", cursor.getString(41)); 
      map.put("cb32", cursor.getString(42)); 
      map.put("cb33", cursor.getString(43)); 
      map.put("cb34", cursor.getString(44)); 
      map.put("sug", cursor.getString(45)); 
      map.put("img1", cursor.getString(46));//imagepath 
      map.put("img2", cursor.getString(47));//imagepath 
      map.put("img3", cursor.getString(48));//imagepath 
      map.put("img4", cursor.getStrin(49)); //imagepath 
      wordList.add(map); 
     } while (cursor.moveToNext()); 
    } 
    database.close(); 
    Gson gson = new GsonBuilder().create(); 
    //Use GSON to serialize Array List to JSON 
    return gson.toJson(wordList); 
} 

例路徑 內容:/com.android.providers.media.documents/document/image%3A20100

MainActivity.java //要上傳

public void syncSQLiteMySQLDB(){ 
    AsyncHttpClient client = new AsyncHttpClient(); 
    RequestParams params = new RequestParams(); 
    ArrayList<HashMap<String, String>> userList = controller.getAllUsers(); 
    if(userList.size()!=0){ 
     if(controller.dbSyncCount() != 0){ 
      prgDialog.show(); 
      params.put("home3JSON", controller.composeJSONfromSQLite()); 
      client.post("http://homeclean.xp3.biz/insertuser.php",params ,new AsyncHttpResponseHandler() { 
       @Override 
       public void onSuccess(String response) { 
        System.out.println(response); 
        prgDialog.hide(); 
        try { 
         JSONArray arr = new JSONArray(response); 
         System.out.println(arr.length()); 
         for(int i=0; i<arr.length();i++){ 
          JSONObject obj = (JSONObject)arr.get(i); 
          System.out.println(obj.get("id")); 
          System.out.println(obj.get("status")); 
          controller.updateSyncStatus(obj.get("id").toString(),obj.get("status").toString()); 
         } 
         Toast.makeText(getApplicationContext(), "DB Sync completed!", Toast.LENGTH_LONG).show(); 
        } catch (JSONException e) { 
         // TODO Auto-generated catch block 
         Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show(); 
         e.printStackTrace(); 
        } 
       } 

〔實施例上傳

phpmyadmin

我沒有我想知道如何做到這一點。請幫助我。

我使用的代碼從

http://programmerguru.com/android-tutorial/how-to-sync-sqlite-on-android-to-mysql-db/

+1

重複:http://stackoverflow.com/questions/20322528/uploading-images-to-server-android – javadaskari

+0

檢查:https://www.youtube.com/watch?v=4LCnoVqQ6N4 – javadaskari

+0

這:http://stackoverflow.com/questions/25300899/how-to-upload-images-from-android-sqlite-to-local-server-folder-using-json – javadaskari

回答

0

首先,你應該將圖像保存到一個變量:

File imgFile = new File("/sdcard/Images/test_image.jpg"); 

if(imgFile.exists()){ 

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath()); 

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest); 

    myImage.setImageBitmap(myBitmap); 

} 

,包括此權限清單文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

然後根據此視頻上傳它: https://www.youtube.com/watch?v=4LCnoVqQ6N4

相關問題