使用Async将位图加载到对象

时间:2013-05-31 23:04:44

标签: android android-asynctask

我遇到了一个问题,我正在尝试从网络API加载图片。我可以做到但是当我尝试将它们放入对象的属性时它只会崩溃。我已经看过如何ASYNC加载图像我可以找到的所有示例,但每个人都找到了将其分配给imageview而不是对象的内容。以下是我来源的一部分。

以下是调用代码的示例

     Movie Example = new Movie();       
     DownloadImageTask task = new DownloadImageTask();
                String path = "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original" + example.getProfile_path();
                example.setProfile(task.execute(path).get());

以下是我用来获取图像的方法

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

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

    protected void onPreExecute() {

    }

    protected Bitmap doInBackground(String... urls) {
        try{
        URL url = new URL(urls[0]);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap result = BitmapFactory.decodeStream(input);
        return result;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    protected void onPostExecute(Bitmap result) {
        //set image of your imageview
        try {
            bmImage.setImageBitmap(result);
            //close

        } catch (Exception ex) {
            Log.e("ERROR", "can't assign image to view");
        }

    }
}

我想要类似的东西,但不是分配给imageview,它会将图像返回给调用它的方法。

1 个答案:

答案 0 :(得分:1)

实际上将图像返回到调用它的方法没有多大意义,因为这种方法必须暂停自身并等待图像加载,这使得图像无法并行加载。你到底想要完成什么?

如果你真的想这样做,我想可以通过修改你的AsyncTask来实现:

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

    private boolean finished = false;
    private Bitmap resultingBitmap = null;

    protected Void doInBackground(String... urls) {
        try{
        URL url = new URL(urls[0]);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        resultingBitmap = BitmapFactory.decodeStream(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
        finished = true;
    }

    public boolean isFinished() {
        return finished;
    }

    public Bitmap getResultingBitmap() {
        return resultingBitmap;
    }
}

在来电者代码中:

DownloadImageTask task = new DownloadImageTask();
String path = "http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original" + example.getProfile_path();
while (!task.isFinished()) { } // this defeats the purpose of parallel execution
example.setProfile(task.execute(path).getResultingBitmap());

然而,您可以按@Brandon的回答中所述完成此操作:

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    Example example;

    public DownloadImageTask(Example example) {
        this.example = example;
    }

    (...)

    protected void onPostExecute(Bitmap result) {
        example.setProfile(result);
    }
}