2012-07-25 149 views
4

美好的一天!我正在嘗試搜索將Android圖像文件上傳到在線MySQL數據庫的基本教程,但找不到任何內容。將Android的圖像上傳到MySQL數據庫

我現在正在開展一項活動,可以將用戶的個人資料圖片從Android上傳到在線服務器。

我需要的是像顯示一個按鈕,當它被點擊時,用戶可以從文件中選擇一個圖像。有人可以指導我這樣做嗎?提前致謝!

+1

試圖搜索...和你有什麼嘗試,直到現在? – Lucifer 2012-07-25 05:54:25

+0

你可以按照這個教程,它必須幫助你 [上傳圖片](http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post -multi-part /)[圖片上傳活動](http://vikaskanani.wordpress.com/2011/01/29/android-image-upload-activity/) – Furqi 2012-07-25 05:58:48

回答

2

在客戶端,您可以執行此操作。

HttpURLConnection connection = null; 
DataOutputStream outputStream = null; 
DataInputStream inputStream = null;  

String pathToOurFile = "path of the image.jpeg"; 
String urlServer = "http://xxx.xxx.xxx.xxx/uploader.php"; 
String lineEnd = "\r\n"; 
String twoHyphens = "--"; 
String boundary = "*****"; 

int bytesRead, bytesAvailable, bufferSize; 
byte[] buffer; 
int maxBufferSize = 1*1024*1024; 

try { 
     FileInputStream fileInStream = new FileInputStream(new File(pathToOurFile)); 

     URL url = new URL(urlServer); 
     connection = (HttpURLConnection) url.openConnection(); 

     // Allow Inputs & Outputs 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setUseCaches(false); 

     // Enable POST method 
     connection.setRequestMethod("POST"); 

     connection.setRequestProperty("Connection", "Keep-Alive"); 
     connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

     outputStream = new DataOutputStream(connection.getOutputStream()); 
     outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
     outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
     outputStream.writeBytes(lineEnd); 

     bytesAvailable = fileInStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 

     // Read file 
     bytesRead = fileInStream.read(buffer, 0, bufferSize); 

     while (bytesRead > 0) 
     { 
      outputStream.write(buffer, 0, bufferSize); 
      bytesAvailable = fileInStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      bytesRead = fileInStream.read(buffer, 0, bufferSize); 
     } 

     outputStream.writeBytes(lineEnd); 
     outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

     // Responses from the server (code and message) 
     serverResponseCode = connection.getResponseCode(); 
     serverResponseMessage = connection.getResponseMessage(); 

     fileInputStream.close(); 
     outputStream.flush(); 
     outputStream.close(); 
    } 
    catch (Exception ex) 
    { 
     //Exception handling 
    } 

服務器端

<?php 
    $target_path = "./"; 
    $target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
     echo "Success"; 
    } else{ 
     echo "Error"; 
    } 
?> 
+0

其實我從某處找到了這段代碼,並沒有有它的鏈接所以發佈了我在我的App.Hope中使用的代碼,它可能也解決了你的問題..警告。 – 2012-07-25 06:01:54

+0

serverResponseCode,serverResponseMessage,fileInputStream。我不知道這些變量做了什麼。我想嘗試你的代碼,但這些變量給我錯誤。 – JetPro 2012-07-25 06:06:20

+0

將它們全部聲明爲字符串變量 – 2012-07-25 06:07:49

1

使用下面的代碼從ImageGallery獲取圖像:

Intent i = new Intent(Intent.ACTION_PICK, 
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(i, ACTIVITY_SELECT_IMAGE); 

它將開始ImageGallery,現在你可以選擇圖像,並在onActivityResult您可以將圖像解碼爲位圖,如鏈接所示:here

protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
    super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

    switch(requestCode) { 
    case REQ_CODE_PICK_IMAGE: 
     if(resultCode == RESULT_OK){ 
      Uri selectedImage = imageReturnedIntent.getData(); 
      String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String filePath = cursor.getString(columnIndex); 
      cursor.close(); 


      Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath); 
     } 
    } 
} 

接下來,您需要將該位圖上傳到服務器。要做到這一點,你可以使用Haresh的解決方案。

相關問題