下载图像从URL与空间在Android的路径路径、图像、空间、URL

2023-09-04 02:54:42 作者:烈酒封喉

我想从路径下载图像。 这是我的code:

I want to download image from the path. This is my code:

String urlString = "https://m.xsw88.com/allimgs/daicuo/20230904/6936.png.gif";
url = new URL(urlString.replaceAll(" ", "%20"));
bm = BitmapFactory.decodeStream(url.openConnection().getInputStream());

但它返回我的BM空。任何想法如何使这个code的工作?

But it returns me null in bm. Any ideas how to make this code to work?

推荐答案

的空间都没有问题......我想它与你的网址,它的工作

The spaces are not the problem ... I tried it with your url and it's working

try {
    String urlString = "https://m.xsw88.com/allimgs/daicuo/20230904/6936.png.gif";
    URL url = new URL(urlString.replaceAll(" ", "%20"));

    URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-agent", "Mozilla/4.0");
    connection.connect();

    InputStream input = connection.getInputStream();

    Log.d("#####", "result: " + BitmapFactory.decodeStream(input));
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

最重要的线是 connection.setRequestProperty(用户代理,Mozilla的/ 4.0); 我不知道是什么原因,解决它,但它之前显然已经在这里工作。

The important line is connection.setRequestProperty("User-agent", "Mozilla/4.0"); I don't know why that solves it, but it has obviously worked before here.

 
精彩推荐
图片推荐