0

我在尝试从 Web api 加载图像时遇到问题。我可以做到,但是当我尝试将它们放入对象的属性中时,它只会崩溃。我已经查看了所有关于如何异步加载我能找到的图像的示例,但是每个人我都找到了将其分配给图像视图而不是对象的内容。以下是我的部分来源。

这是调用代码的示例

     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,而是将图像返回给调用它的方法。

4

1 回答 1

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);
    }
}
于 2013-05-31T23:19:06.363 回答