簡體   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