Android从存储在JSONObject中的URL获取图像

时间:2014-01-10 05:47:18

标签: android json android-fragments imageview

我想从一个json数组中的url获取两个图像我在异步任务中使用JSONParser获取JSON数据(url字符串)如何现在加载图像以及在哪里做,我想显示图像另一个片段。我从另一个片段活动中全局声明了2个Drawables。

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

5 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

一种方法是,将ImageView作为参数传递给异步任务,并在onPostExecute中更新它。

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();
    }
}

您可以使用图像加载器库,我建议使用Universal Image Loader(https://github.com/nostra13/Android-Universal-Image-Loader

答案 2 :(得分: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();
}

答案 3 :(得分:0)

Image Loader类,用于在ImageView中加载和显示来自JSONObject的url的图像。

查看this sample

答案 4 :(得分:0)

为什么使用此代码,listview中的图像会连续刷新?

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

}

如果我使用滚动第一次刷新图像并且不能保持静态