English 中文(简体)
利用AsyncTask
原标题:Loading Image using AsyncTask

我正试图制作一个定制清单改编器,该改编器有其从互联网下载所需每一项目的图像。 在我首次进入活动时——在图像下载之前冻结一台,然后装上活动清单。

由于我可以在活动负荷之前下载图像。 在活动负荷后,我如何下载图像。 我认为,我需要利用阿森特任务。 但是,由于我装上了“Array”改编机的图像,我无法确定如何做到这一点。

这是我的习惯改编器getView():

public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.list_item_default, null);
        }

        Item p = items.get(position);

        if (p != null) {

            TextView list_title = (TextView) v.findViewById(R.id.list_title);
            TextView list_description = (TextView) v
                    .findViewById(R.id.list_description);
            TextView list_timestamp = (TextView) v
                    .findViewById(R.id.list_timestamp);
            ImageView list_image = (ImageView) v.findViewById(R.id.list_image);

            if (list_title != null) {
                list_title.setText(p.getItemTitle());
            }

            if (list_description != null) {
                list_description.setText(p.getItemDescription());
            }

            if (list_timestamp != null) {
                list_timestamp.setText(p.getItemTimestamp());
            }

            if (list_image != null) {
                Log.d("bMobile", "inside getView() image");
                try {
                    URL imageURL = new URL(p.getItemImage());
                    HttpURLConnection con = (HttpURLConnection) imageURL
                            .openConnection();
                    InputStream inputStrem = con.getInputStream();
                    Bitmap image = BitmapFactory.decodeStream(inputStrem);
                    if (null != image)
                        list_image.setImageBitmap(image);
                    else
                        Log.d("bMobile", "Bitmap is Null");
                } catch (Exception e) {
                }
            }
        }

        return v;
    }

AsyncTask:

public class DownloadImagesTask extends AsyncTask<ImageView, Void, Bitmap> {

    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView... imageViews) {
        this.imageView = imageViews[0];
        return download_Image((String)imageView.getTag());
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        imageView.setImageBitmap(result);
    }

}
    private Bitmap download_Image(String url) {

        Bitmap bmp =null;
        try{
            URL ulrn = new URL(url);
            HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
            InputStream is = con.getInputStream();
            bmp = BitmapFactory.decodeStream(is);
            if (null != bmp)
                return bmp;

            }catch(Exception e){}
        return bmp;
    }
最佳回答

“镜头冻结”——这是因为你正在把图像下载到天线或天线上。 你们永远不应这样做。 所有封锁行动、长期行动、网络行动都应单独进行。 是的,你可以把图像装上,以解决这个问题。

你可以采取两种办法解决这一问题:

  1. Use WebImageView from droid fu library at http://brainflush.wordpress.com/2009/11/23/droid-fu-part-2-webimageview-and-webgalleryadapter/ WebImageView uses caching as well so you dont need to download images again which you have already downloaded. Downloading images are expensive and consume data. I used WebImageView and was able to load images in list. It took me just about 20 mins to get it all working, so you know its easy integrating it.

  2. Second option is to use async task . Please check Android : Loading an image from the Web with Asynctask . There are lot more info in stackoverflow regarding how to do this.

您的国祥之.,你们应当有一种令人信服的感觉,即你们在国祥之地做一些事情。

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签