【问题标题】:Getting java.net.MalformedURLException: Protocol not found while downloading image using asynctask in android获取 java.net.MalformedURLException:在 android 中使用 asynctask 下载图像时找不到协议
【发布时间】:2023-04-03 12:04:01
【问题描述】:

在android中使用Asynctask下载图像时找不到java.net.MalformedURLException:协议

我的代码是:

    private Bitmap downloadImage(String url) {

        HttpURLConnection urlConnection = null;
        try {


            URL uri = new URL(url);
            urlConnection = (HttpURLConnection) uri.openConnection();
            int statusCode = urlConnection.getResponseCode();
            if (statusCode != 200) {
                return null;
            }

            InputStream inputStream = urlConnection.getInputStream();
            if (inputStream != null) {
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (MalformedURLException e) {
            System.out.println("MalformedURLException --"+e.toString());
        //   urlConnection.disconnect();

        }
        catch (Exception e) {
            System.out.println("Exception --"+e.toString());
        }
        finally
         {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
        }
        return null;
    }


}

请帮忙。提前致谢。

【问题讨论】:

  • 一个更简单的建议是使用Picasso库来缓存和加载来自url或文件的图像。
  • 在浏览器上粘贴你的url,看看url中是否有不支持的字符
  • 检查您的网址,是否格式正确.. 可能是 http:// 丢失

标签: android


【解决方案1】:

错误:

java.net.MalformedURLException:找不到协议

您收到此错误是因为您的 URL 没有协议组件。

它需要http://https:// 或您想要的任何其他协议。

希望对你有帮助。

【讨论】:

猜你喜欢
  • 2018-11-08
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 2020-04-20
  • 2010-12-14
  • 2014-05-26
  • 1970-01-01
相关资源
最近更新 更多