【问题标题】:Load image from Google Places in Android从 Android 中的 Google 地方信息加载图像
【发布时间】:2017-05-02 09:08:21
【问题描述】:

我在使用 Google Places API 将 URL 中的图片加载到我的 Android 应用时遇到问题。

我得到了 API 工作的部分,如果我在浏览器中输入 URL,我可以看到图像。代码在以下行中失败: 位图 myBitmap = BitmapFactory.decodeStream(input);

奇怪的是,它只是直接跳转到catch子句中的return null,而不执行println命令。

我还应该解释一下,我使用了两个 URL,因为 google API 中的第一个 URL 是重定向页面。

我认为问题在于正在加载的最终页面实际上不是“真实”图像(我的意思是它看起来像 https://lh4.googleusercontent.com/XXX/s1600-w400/ 而不是类似的东西 http://something/image.jpg)

下面是我使用的整个方法:

public  Bitmap getImageData(Place p) {
    try {
        String src= "https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference="+
                p.getPhoto_reference()+
                "&key="+
                this.context.getResources().getString(places_api);
        Log.d("srcsrc", src);
        URL url = new URL(src);
        HttpsURLConnection ucon = (HttpsURLConnection) url.openConnection();
        ucon.setInstanceFollowRedirects(false);
        URL secondURL = new URL(ucon.getHeaderField("Location"));
        HttpsURLConnection connection = (HttpsURLConnection) secondURL.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap; 
    }
    catch (Exception e)
    {
        e.printStackTrace();
        System.out.println("Bitmap exception" + e);
        return null;
    }
}

感谢您的帮助。

【问题讨论】:

标签: android imageview google-places-api


【解决方案1】:

像这样使用毕加索:

            Picasso.with(context).load(src).into(imageView );

它将执行以下操作:

在适配器中处理 ImageView 回收和下载取消。 使用最少的内存进行复杂的图像转换。 自动内存和磁盘缓存。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-09
    • 1970-01-01
    • 2020-01-28
    • 2018-04-06
    • 2015-03-10
    • 1970-01-01
    相关资源
    最近更新 更多