【问题标题】:Android download image from shorturlAndroid 从shorturl 下载图片
【发布时间】:2016-01-10 01:11:23
【问题描述】:

我正在尝试从this 给定短网址/重定向从 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;
    }
}

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

【问题讨论】:

  • 你的短网址坏了吗?
  • 不行,为什么要破?
  • 我不知道,从它的声音来看,你是说它适用于长网址但不适用于短网址,所以我认为它可能会被破坏
  • 可能是因为 http 重定向
  • 是的,只是这个重定向是我的问题:/

标签: android facebook download short-url


【解决方案1】:

尝试使用 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
滑翔:https://github.com/bumptech/glide

【讨论】:

    猜你喜欢
    • 2015-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多