1

我正在开发一个需要使用 Multipart 上传图片的项目。这是我的代码:

public void doUploadGIF(File image) {

        try {
            long start = System.currentTimeMillis();
            HttpPost httppost = new HttpPost(
                    "myurl");
            MultipartEntity multipartEntity = new MultipartEntity(
                    HttpMultipartMode.BROWSER_COMPATIBLE);

            multipartEntity.addPart("gif", new FileBody(image));
            httppost.setEntity(multipartEntity);

            mHttpClient.execute(httppost, new PhotoUploadResponseHandler());

            long end = System.currentTimeMillis();
            System.out.println("Time for uploading:"
                    + String.valueOf(end - start));
        } catch (Exception e) {
            Log.e("Upload", "Error while uploading");
        }
    }

private class PhotoUploadResponseHandler implements ResponseHandler<Object> {

        @Override
        public Object handleResponse(HttpResponse response)
                throws ClientProtocolException, IOException {

            HttpEntity r_entity = response.getEntity();
            String responseString = EntityUtils.toString(r_entity); 
            Log.d("UPLOAD", responseString);

            return null;
        }

    }

现在在客户端(Android)我让用户知道上传的过程(百分比)。我怎样才能做到这一点?感谢您的关注 !

4

1 回答 1

0

在我的例子中,我使用了以 KB 为单位的文件大小,并将进度条的限制设置为以 KB 为单位的文件大小。并更新我使用的进度条:

int bufferLength = 1024;

File file = new File(ImgURI);
mFileLen =  file.length();

int maxBufferSize = 1*1024*1024;

bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);

for (int i = 0; i < bufferSize; i +=bufferLength) {
    progress = i/1024;
    progressBar1.setMax(kbfilesize);
    publishProgress(progress);
} 
于 2013-07-16T03:01:22.863 回答