2011-05-11 3 views
0

안녕하세요 저는이 클래스를 사용하여 이미지를 업로드하고 있습니다. 문제는 파일이 업로드 될 때까지 전화가 아무 것도하지 않는 것입니다. 예를 들어, 이미지가 1.5MB 크기이고 전화가 '정지 상태'인 경우 약 10 ~ 20 초가 인터넷 속도에 따라 다릅니다. 그것은 가장 느린 모바일이 아니므로 문제는 스크립트에있는 것 같습니다.안드로이드 업로드 - 전화 후행

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

관련 문제