gpt4 book ai didi

android - 将图像上传到Android中的服务器时通知栏中的进度条

转载 作者:搜寻专家 更新时间:2023-11-01 07:39:52 25 4
gpt4 key购买 nike

我正在通过从图库中选择图像或使用 android 相机捕获图像来将图像上传到网络服务器。

我需要在通知栏中显示进度。我看到 Facebook 应用程序执行此操作。有没有办法找出已上传了多少?

我上传图片到服务器的代码是:

public void uploadImage() {
final Toast toast = Toast.makeText(this, "Image uploaded successfully",
Toast.LENGTH_SHORT);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Upload");
builder.setMessage("Do you want to upload the captured picture?");
builder.setIcon(R.drawable.icon);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
testWebService(Bitmap bmp)
finish();
toast.show();

}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

dialog.cancel();

}
});
AlertDialog alert = builder.create();
alert.show();

}



public void testWebService(Bitmap bmp) {

MarshalBase64 marshal = new MarshalBase64();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bmp.compress(CompressFormat.PNG, 100, out);
byte[] raw = out.toByteArray();

SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
OPERATION_NAME);

request.addProperty("image", raw);

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
marshal.register(envelope);
HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);

try

{

httpTransport.call(SOAP_ACTION, envelope);
Object response = envelope.getResponse();
}

catch (Exception exception)

{
exception.printStackTrace();

}

}

有没有办法知道上传了多少?

最佳答案

这是有用的链接:http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView

现在在您的通知自定义布局中使用进度条。

此代码用于跟踪下载。因此您可以检查上传状态,在服务中启动此异步任务以相应地上传和更新进度条

 private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
}
return totalSize;
}

protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}

protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}

关于android - 将图像上传到Android中的服务器时通知栏中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5440474/

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