开发者

Android upload - phone lagging

开发者 https://www.devze.com 2023-03-04 22:42 出处:网络
Hello I\'m using this class to upload a image. The problem is that the phone stops to do ANYTHING untill the file is uploaded. For example a image is large 1.5MB and phone \'freezes\' for about 10~20

Hello I'm using this class to upload a image. The problem is that the phone stops to do ANYTHING untill the file is uploaded. For example a image is large 1.5MB and phone 'freezes' for about 10~20 seconds depends of the internet speed. It's not the slowest mobile, so I guess that the problem is in the script.

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;
    }
}


All operations run on UI thread unless you do something with this. The only option is to do client-server communication in separate thread. Refer to this tutorial

0

精彩评论

暂无评论...
验证码 换一张
取 消