繁体   English   中英

Android从Shorturl下载图像

[英]Android download image from shorturl

我试图从下载图像给短网址/从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;
    }
}

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

尝试改用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 Universal Image Loader: https//github.com/nostra13/Android-Universal-Image-Loader
离子: https//github.com/koush/ion
滑行: https : //github.com/bumptech/glide

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM