如何在android中的json数据上显示ImageView上的图像

Om3*_*3ga 5 java android android-layout

我正在获取json数据.在那个json我有一个图像的URL.现在我想在ImageView中显示该Image.我怎么能做到这一点?这是我的代码

class LoadInbox extends AsyncTask<String, String, String> {
    /**
     * Before starting background thread Show Progress Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Home.this);
        pDialog.setMessage("Loading Inbox ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * getting Inbox JSON
     * */
 protected String doInBackground(String... arg0) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        JSONObject json = userFunctions.homeData();

        Log.e("Data", json.toString());

        // Check your log cat for JSON reponse
        Log.d("Inbox JSON: ", json.toString());

        try {
            data = json.getJSONArray(TAG_DATA);
            Log.d("inbox array: ", data.toString());
            // looping through All messages
            for (int i = 0; i < data.length(); i++) {
                JSONObject c = data.getJSONObject(i);

                // Storing each json item in variable
                String profile_img = c.getString(TAG_PROFILE_IMG);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                map.put(TAG_PROFILE_IMG, profile_img);

                // adding HashList to ArrayList
                inboxList.add(map);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
        // updating UI from Background Thread
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */
                ListAdapter adapter = new SimpleAdapter(
                        Home.this, inboxList,
                        R.layout.home_list_item, new String[] { TAG_PROFILE_IMG },
                        new int[] { R.id.profile_img2 });
                // updating listview
                setListAdapter(adapter);
            }
        });

    }
Run Code Online (Sandbox Code Playgroud)

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView 
    android:id="@+id/profile_img2" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingTop="8dip"
    android:paddingLeft="8dip"
    android:paddingBottom="4dip" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

chu*_*ubs 5

所以你需要创建另一个 AsyncTask 给定一个 URL 将加载图像,并填充一些控件。我通常做这样的事情:

ImageView imageView = (ImageView)findById(R.id.blah);
new ImageLoader( person.getImageUri(), imageView, 128, 128 ).execute();
Run Code Online (Sandbox Code Playgroud)

ImageLoader 将是另一个像这样的 AsyncTask:

public class ImageLoader extends AsyncTask<URI,Integer,BitmapDrawable> {
    private Uri imageUri;

    private ImageView imageView;

    private int preferredWidth = 80;
    private int preferredHeight = 80;

    public ImageLoader( URI uri, ImageView imageView, int scaleWidth, int scaleHeight ) {
        this.imageUri = uri;
        this.imageView = imageView;
        this.preferredWidth = scaleWidth;
        this.preferredHeight = scaleHeight;
    }

    public BitmapDrawable doInBackground(URI... params) {
    if( imageUri == null ) return null;
    String url = imageUri.toString();
    if( url.length() == 0 ) return null;
    HttpGet httpGet = new HttpGet(url);
    DefaultHttpClient client = new DefaultHttpClient();
    HttpResponse response = client.execute( httpGet );
    InputStream is = new BufferedInputStream( response.getEntity().getContent() );
    try {
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        if( preferredWidth > 0 && preferredHeight > 0 && bitmap.getWidth() > preferredWidth && bitmap.getHeight() > preferredHeight ) {
            return Bitmap.createScaledBitmap(bitmap, preferredWidth, preferredHeight, false);
        } else {
            return bitmap;
        }
    } finally {
        is.close();
    }
    }
}

    public void onPostExecute( BitmapDrawable drawable ) {
        imageView.setImageBitmap( drawable );
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以通过创建自己的子类 ListAdapter 在图像绑定到 ListView 时关闭此 AsyncTask。所以你必须放弃使用 SimpleAdapter 因为事情不再简单了。这有很多优点,因此您只需加载正在显示的图像。这意味着从总数中加载了非常小的数字。此外,您的用户可以在图像加载之前查看数据,从而更快地访问数据。如果您在现有的 AsyncTask 中执行此操作,您将加载每个图像,并且用户必须等待每个图像完成才能向用户显示数据。有一些事情可以通过这个改进。一个 AsyncTask 使用自己的线程,因此您可能会同时运行许多线程(10 个或更多)。这可能会杀死具有大量客户端的服务器。您可以使用 ExecutorService(即线程池)集中这些,但您必须放弃使用 AsyncTask 并实现您自己的工具以从 UI 线程运行作业并将结果发布回 UI 线程。其次,每次用户滚动时,您的图像都会加载。为此,我基于图像的 URI 实现了自己的缓存方案,因此我只加载图像一次并从缓存中返回它。在这里发布的代码有点太多,但这些是为读者提供的练习。为此,我基于图像的 URI 实现了自己的缓存方案,因此我只加载图像一次并从缓存中返回它。在这里发布的代码有点太多,但这些是为读者提供的练习。为此,我基于图像的 URI 实现了自己的缓存方案,因此我只加载图像一次并从缓存中返回它。在这里发布的代码有点太多,但这些是为读者提供的练习。

另请注意,我没有在 onPostExecute() 中回发到 UI 线程。那是因为 AsyncTask 为我做了这件事,我不必再像上面的代码所示那样做。您应该删除额外的 runnable 并内联 onPostExecute() 中的代码。