简体   繁体   English

如何从Android上的asynctask返回图像

[英]How to return images from asynctask on Android

I am writing an application where I am building a list view in which each item has an image. 我正在编写一个应用程序,在其中构建一个列表视图,其中每个项目都有一个图像。 The content of the list view is generated by an XML file that is stored locally, the pictures are fetched from Amazon AWS S3. 列表视图的内容由本地存储的XML文件生成,图片从Amazon AWS S3中获取。

I want to write a class AWSImageFetcher that will be responsible for the authentication first (by using another dedicated class) and then for fetching the images. 我想编写一个类AWSImageFetcher ,它将首先负责身份验证(通过使用另一个专用的类),然后负责获取图像。

I understood that it is best practice on Android to subclass AsyncTask in cases like this to perform the network requests. 我了解,在此类情况下,最好在Android上将AsyncTask子类化以执行网络请求。 I am now wondering how I should return the images from the AWSImageFetcher class to the list view. 我现在不知道我应该怎么从返回的图像AWSImageFetcher类列表视图。

I am coming from iOS where I would just write a delegate for the AWSImageFetcher which would be invoked after the images have been fetched, but this doesn't feel right on Android. 我来自iOS,我将为AWSImageFetcher编写一个委托,该AWSImageFetcher将在获取图像后调用,但是在Android上感觉不对。 Should I use a listener class instead? 我应该改用监听器类吗? How would you solve a situation like this on Android in an elegant way? 您将如何以一种优雅的方式在Android上解决这种情况?

Try this form 试试这个表格

onPreExecute executes first in UIThread, later doInbakground function execute in parellel thread then after that postExecute run in UIThread onPreExecute首先在UIThread中执行,之后doInbakground函数在并行线程中执行,然后在UIThread中运行postExecute之后

private class AWSImageFetcher extends AsyncTask<String, Void, Bitmap> 
{
    boolean authenticated;

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
        authenticated=authenticate();
    }

    @Override
    protected Bitmap doInBackground(String... urls) 
    {
        Bitmap b=null;
        if(authenticated)
        {
                URL imageUrl = new URL(urls[0]);
            HttpURLConnection conn = (HttpURLConnection) imageUrl
                    .openConnection();
            conn.setConnectTimeout(TIME_OUT_IN_MILLI_SECONDS);
            conn.setReadTimeout(TIME_OUT_IN_MILLI_SECONDS);
            conn.setInstanceFollowRedirects(true);
            InputStream is = conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            b = decodeFile(f);
        }
        return b;
    }

    @Override
    protected void onPostExecute(Bitmap result) 
    {
        super.onPostExecute(result);
        if(result!=null)
        {               
            //use bitmap image in result    
        }
       else
       {   
          //Image is not available
       }    

    }

 }


//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
    try {
        // decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        FileInputStream stream1 = new FileInputStream(f);
        BitmapFactory.decodeStream(stream1, null, o);
        stream1.close();

        // Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE = 70;
        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE
                    || height_tmp / 2 < REQUIRED_SIZE)
                break;
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        // decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        FileInputStream stream2 = new FileInputStream(f);
        Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
        stream2.close();
        return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM