2011-05-11 56 views
0

你好我正在使用這個類上傳圖片。問題是電話停止做任何事情,直到文件上傳。例如,一張圖片大小爲1.5MB,根據網速,手機會「凍結」10到20秒。這不是最慢的手機,所以我想這個問題在腳本中。Android上傳 - 手機滯後

package com.project; 

import java.io.DataInputStream; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.app.Activity; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.util.Log; 

public class upload extends Activity { 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.post); 


} 

    } 
class UploadClass { 
    String pathToOurFile; 
    String getId; 
boolean UploadFile() { 

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

    String urlServer = "http://path.to-my.site/upload.php?byId="+getId; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 

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

    try 
    { 
    FileInputStream fileInputStream = 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 = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    buffer = new byte[bufferSize]; 

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

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

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

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

    fileInputStream.close(); 
    outputStream.flush(); 
    outputStream.close(); 

    } 
    catch (Exception ex) 
    { 
     return false; 
    } 
    return true; 
    } 
} 

回答

2

所有操作都在UI線程上運行,除非您對此做了某些操作。唯一的選擇是在單獨的線程中進行客戶端 - 服務器通信。 參照此tutorial