2014-01-10 63 views
0

我想從json數組中獲取兩個圖像我在異步任務中使用JSONParser獲取JSON數據(url字符串)如何現在加載圖像以及在何處執行此操作,我想在另一個片段中顯示圖像。我申報2個Drawables Globallly從另一個片段活動訪問。Android從存儲在JSONObject中的URL獲取圖像

MainActivity.java

@Override 
protected JSONObject doInBackground(String... args) { 
    JSONParser jParser = new JSONParser(); 

    // Getting JSON from URL 
    JSONObject json = jParser.getJSONFromUrl(url); 
    return json; 
} 
@Override 
protected void onPostExecute(JSONObject json) { 
    try {     
      String Student1Image = json.getString("url1"); 
      String Student2Image = json.getString("url1"); 

      } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

現在在哪裏以及如何裝入圖像另一個片段

MainScreen.java(另一片段)

public class MainScreen extends Fragment { 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View rootView = inflater.inflate(R.layout.main, container, false); 
    new MainActivity(); 

    ImageView Image = (ImageView) rootView.findViewById(R.id.imgMainWeather);    
    Image.setImageDrawable(**What to Give Here**); 

    return rootView; 
    } 

回答

0

一個要顯示的方法是將ImageView作爲參數傳遞給異步任務並將其更新PostExecute。

public class CustomAsyncTask extends AsyncTask<Void, Void, Void> { 
    private ImageView image; 

    private ProgressAsyncTask(ImageView image) { 
     this.image = image; 
    } 

    // ... other things here 

    @Override 
    protected void onPostExecute(JSONObject json) { 
     try {     
      String Student1Image = json.getString("url1"); 
      String Student2Image = json.getString("url1"); 

      // update image view here 
     } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
} 

您可以使用圖像裝載庫,我建議通用圖像裝載機(https://github.com/nostra13/Android-Universal-Image-Loader

+0

我retrived的URL只是我想知道如何從URL和使用什麼方法和在那裏檢索圖像去做吧。 – Prashanth

+0

你可以使用一個圖像加載器庫,我最喜歡的是這個:https://github.com/nostra13/Android-Universal-Image-Loader – ayorhan

1

試試這個:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
    ImageView bmImage; 

    public DownloadImageTask(ImageView bmImage) { 
     this.bmImage = bmImage; 
    } 

    protected Bitmap doInBackground(String... urls) { 
     String urldisplay = urls[0]; 
     Bitmap mIcon11 = null; 
     try { 
      InputStream in = new java.net.URL(urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      Log.e("Error", e.getMessage()); 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 

    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 
} 
0

試試這個

try { 
    ImageView i = (ImageView)rootView.findViewById(R.id.image); 
    Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); 
    i.setImageBitmap(bitmap); 
} catch (MalformedURLException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
0

圖像裝載機類從ImageView中加載和顯示來自JSONObject的url的圖像。

看一看this sample

0

爲什麼使用此代碼,從列表視圖圖像刷新持續?

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> { 
ImageView bmImage; 

public DownloadImageTask(ImageView bmImage) { 
    this.bmImage = bmImage; 
} 

protected Bitmap doInBackground(String... urls) { 
    String urldisplay = urls[0]; 
    Bitmap mIcon11 = null; 
    try { 
     InputStream in = new java.net.URL(urldisplay).openStream(); 
     mIcon11 = BitmapFactory.decodeStream(in); 
    } catch (Exception e) { 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 
    return mIcon11; 
} 

protected void onPostExecute(Bitmap result) { 
    bmImage.setImageBitmap(result); 
} 

}

,如果我使用滾動第一圖像再次刷新和無法留靜態

相關問題