在我的应用程序中,用户将图像(转换为 PDF)上传到我们的服务器,我遇到的问题是上传对某些用户不起作用,在 wifi 或移动数据上,
该应用程序在我的设备,几个模拟器和其他几个物理设备上完美运行,但上传对某些用户不起作用,我的意思是进度对话框出现并立即消失,好像没有正在处理的数据,这种情况偶尔发生,我对此完全感到困惑,我正在使用 Koush-ION 进行上传,其他与网络相关的功能(使用 Volley)工作得非常好,比如从服务器中提取 JSON 数据以进行下拉等。
这是上传和图像到 PDF 转换的代码 ''' private void uploadImageToServer() {
final ProgressDialog pd=new ProgressDialog(SecondActivity.this);
pd.setMessage("Uploading, Please Wait....");
pd.setCanceledOnTouchOutside(false);
pd.show();
PdfDocument document=new PdfDocument();
// crate a page description
PdfDocument.PageInfo pageInfo;
PdfDocument.Page page;
Canvas canvas;
int i;
Bitmap image;
for (i=0; i < list.size(); i++) {
pageInfo=new PdfDocument.PageInfo.Builder(992, 1432, 1).create();
page=document.startPage(pageInfo);
canvas=page.getCanvas();
image=BitmapFactory.decodeFile(list.get(i));
image = Bitmap.createScaledBitmap(image, 980, 1420, true);
image.setDensity(DisplayMetrics.DENSITY_300);
canvas.setDensity(DisplayMetrics.DENSITY_300);
canvas.drawBitmap(image, 0, 0, null);
document.finishPage(page);
}
@SuppressWarnings("deprecation") String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file=new File(directory_path);
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.mkdirs();
}
@SuppressLint("SimpleDateFormat") String timeStamp=(new SimpleDateFormat("yyyyMMdd_HHmmss")).format(new Date());
String targetPdf=directory_path + timeStamp + ".pdf";
File filePath=new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
} catch (IOException e) {
Log.e("main", "error " + e.toString());
Toasty.error(this, "Error making PDF" + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
CheckBox chk=findViewById(R.id.chk1);
if (chk.isChecked()) {
Uri.Builder builder=new Uri.Builder();
builder.scheme("https")
.authority("www.smartpractice.co.za")
.appendPath("files-upload-phone-app.asp")
.appendQueryParameter("MyForm", "Yes")
.appendQueryParameter("ClientID", clientId)
.appendQueryParameter("Username", email)
.appendQueryParameter("Pwd", pwd)
.appendQueryParameter("Category", Item)
.appendQueryParameter("ClientName", Item2)
.appendQueryParameter("NoEmail", "Yes");
myURL=builder.build().toString();
} else {
Uri.Builder builder4=new Uri.Builder();
builder4.scheme("https")
.authority("www.smartpractice.co.za")
.appendPath("files-upload-phone-app.asp")
.appendQueryParameter("MyForm", "Yes")
.appendQueryParameter("ClientID", clientId)
.appendQueryParameter("Username", email)
.appendQueryParameter("Pwd", pwd)
.appendQueryParameter("Category", Item)
.appendQueryParameter("ClientName", Item2)
.appendQueryParameter("NoEmail", "");
myURL=builder4.build().toString();
}
Ion.with(SecondActivity.this)
.load(myURL)
.setTimeout(360000000)
.uploadProgressDialog(pd)
.setMultipartFile("pdf","pdf/*",filePath )
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>()
{
@Override
public void onCompleted(Exception e, JsonObject result) {
String message = result.get("message").getAsString();
Toasty.info(SecondActivity.this, message, Toast.LENGTH_LONG).show();
Button upload=findViewById(R.id.upload);
upload.setText(message);
pd.cancel();
}
});
}