Nativescript 插件的异步问题,强制使用 .get
Async issues on Nativescript plugin, forced to use .get
我在 java class 中有一个 JSONobject,它从我的 Nativescript 插件中调用。
它被发送一个 url 以下载并保存在 phone 上(保存 class 未显示)然后 returns 一个包含高度和宽度的对象return 返回插件。
我想显示进度对话框,但是在使用 Async 和 .get 时这是不可能的,我看不到任何其他方式来下载图像和 return JSONObject 以等待来自 onPostExecute 的插件()
public JSONObject downloadImage(Context context, String url) throws ExecutionException, InterruptedException, JSONException {
Save.ReturnObj returnObj;
DownloadImage downloadImage = new DownloadImage();
downloadImage.execute(imageandUrl);
Bitmap bitmap = downloadImage.get(); // would like to do this without .get
JSONObject obj = new JSONObject();
returnObj = savefile.SaveImage(); //saves image and returns image heights
//get dimesnions
obj.put("h", returnObj.h);
obj.put("w", returnObj.w);
return obj; //returns this object to plugin
}
这里是异步函数。每个人都说将所有代码放在 onPostExecute() 部分,但这是如何工作的,我需要 public JSONObject downloadImage 到 return 基于维度的对象,我不能从这里做到这一点。
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
private int imagenumber;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(Strng... imageUrl) {
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
//It is redundant here, I would like this to do what happens in the public JSONObject downloadImage JSON
}
}
有nativescript-downloadmanager
- https://www.npmjs.com/package/nativescript-downloadmanager 插件,可以帮助您下载文件。该插件将在下载图像时显示进度条。
我在 java class 中有一个 JSONobject,它从我的 Nativescript 插件中调用。
它被发送一个 url 以下载并保存在 phone 上(保存 class 未显示)然后 returns 一个包含高度和宽度的对象return 返回插件。
我想显示进度对话框,但是在使用 Async 和 .get 时这是不可能的,我看不到任何其他方式来下载图像和 return JSONObject 以等待来自 onPostExecute 的插件()
public JSONObject downloadImage(Context context, String url) throws ExecutionException, InterruptedException, JSONException {
Save.ReturnObj returnObj;
DownloadImage downloadImage = new DownloadImage();
downloadImage.execute(imageandUrl);
Bitmap bitmap = downloadImage.get(); // would like to do this without .get
JSONObject obj = new JSONObject();
returnObj = savefile.SaveImage(); //saves image and returns image heights
//get dimesnions
obj.put("h", returnObj.h);
obj.put("w", returnObj.w);
return obj; //returns this object to plugin
}
这里是异步函数。每个人都说将所有代码放在 onPostExecute() 部分,但这是如何工作的,我需要 public JSONObject downloadImage 到 return 基于维度的对象,我不能从这里做到这一点。
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
private int imagenumber;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(Strng... imageUrl) {
Bitmap bitmap = null;
try {
// Download Image from URL
InputStream input = new java.net.URL(imageURL).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
//It is redundant here, I would like this to do what happens in the public JSONObject downloadImage JSON
}
}
有nativescript-downloadmanager
- https://www.npmjs.com/package/nativescript-downloadmanager 插件,可以帮助您下载文件。该插件将在下载图像时显示进度条。