0

我正在尝试从这个给定的短 url/redirect 从 facebook 下载图像,但在我的情况下它不起作用 - 这是我的代码:

private Bitmap getBitmap(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Bitmap btm = BitmapFactory.decodeStream(is);
        is.close();
        return btm;
    }catch (MalformedURLException e) {
        Log.v(Constants.LOG_TAG, "[Error] " + e + ": " + e.getMessage());
        e.printStackTrace();
        return null;
    }catch (IOException e) {
        Log.v(Constants.LOG_TAG, "[Error] " + e + ": " + e.getMessage());
        e.printStackTrace();
        return null;
    }
}

如果我使用长链接,它也可以正常工作,但我需要从短链接中获取这张照片。有谁知道如何解决这个问题?谢谢你。

4

1 回答 1

0

尝试改用 http:

public Bitmap getBitmapFromURL(String src) {
    try {
        java.net.URL url = new java.net.URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

或尝试使用以下库之一,例如:


Android 通用图像加载器:https
://github.com/nostra13/Android-Universal-Image-Loader 离子:https
://github.com/koush/ion Glide:https ://github.com/bumptech/glide

于 2016-01-10T07:26:16.413 回答