2017-01-17 8 views
0

私は、Nativescriptプラグインから呼び出されるJavaクラスの中にJSONオブジェクトを持っています。Nativescriptプラグインの非同期の問題が強制的に使用される.get

URLがダウンロードされて電話に保存され(保存クラスは表示されません)、プラグインに返される高さと幅を含むオブジェクトが返されます。

私は進捗ダイアログを表示したいと思いますが、これは可能ではない非同期とに.getを使用しているとき、私は画像をダウンロードして

onPostExecute()からプラグインを待っているにJSONObjectを返すために、他の方法を見ることができません
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 
} 

ここにansync機能があります。誰もがonPostExecute()の部分にすべてのコードを入れると言っていますが、これはどうすればいいですか?public JSONObject downloadImageがdimesionsに基づいてオブジェクトを返す必要があります。あなたがあなたのファイルをダウンロードするのに役立つ可能性がありhttps://www.npmjs.com/package/nativescript-downloadmanagerプラグイン、 -

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 
    } 
} 

答えて

1

nativescript-downloadmanagerあります。プラグインは画像をダウンロードしている間に進行状況バーを表示します。

関連する問題