gpt4 book ai didi

java - 使用分段上传图像时如何在客户端获取进程

转载 作者:行者123 更新时间:2023-12-01 04:33:21 24 4
gpt4 key购买 nike

我正在开发一个需要使用 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)我让用户知道上传的过程(百分比)。我怎样才能做到这一点?感谢您的关注 !

最佳答案

在我的例子中,我使用以 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);
}

关于java - 使用分段上传图像时如何在客户端获取进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17667139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com