我面对java.net.malformedurlexception:无协议:将URL转换为Android中的位图时

发布于 2025-02-14 01:07:19 字数 2549 浏览 1 评论 0 原文

我正在尝试将URL转换为位图,然后将该位图设置为墙纸。所有这些过程都通过在Android中使用Worker类在后台完成。但是我没有协议错误。 然后我尝试通过将其转换为位图,

  try {
                        URL url = new URL(image_url);
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                        myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);

                    } catch(IOException e) {
                        Log.e("tag102",e.getMessage());
                    }

但是这种方法给了我LogCat中的错误

2022-07-08 03:15:07.544 25513-25752/com.nightowl.stylo E/tag102: no protocol: 

我正在从firebase获取任何一个随机墙纸链接, 任何错误。壁纸也会更改,

 try {
                        URL url = new URL("https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800");
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                        myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);

                    } catch(IOException e) {
                        Log.e("tag102",e.getMessage());
                    }

但我希望随机字符串设置并从中获得位图。那我如何实现呢? 编码URL

 try {
        encodedURL = URLEncoder.encode(image_url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.e("tag3",e.toString());
    }

我还尝试通过我在此项目中使用三种URL

  1. https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto = compress&cs = tinysrgb; amp; amp; amp; amp; a>
  2. https://cdn.pixabay.com/photo/2020/11/27/22/07/naruto-5783102_960_720.png
  3. https://images.unsplash.com/photo-16414141414315243-196E7382C32D?IXLIB=rb-1.2.1.1.2.1&ixid= mnwxmja3fdb8mhxwag90by1wywdlfhx8fgvufdb8fhx8& auto =格式=格式& fit = crop& w = 1178&q = 80

任何帮助将不胜感激。

I am trying to convert url to bitmap and then set that bitmap to background as wallpaper. And all this process is getting done in background with the use of worker class in android. But I am getting no protocol error. I am fetching any one random wallpaper link from firebase then I am trying to convert it into bitmap by

  try {
                        URL url = new URL(image_url);
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                        myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);

                    } catch(IOException e) {
                        Log.e("tag102",e.getMessage());
                    }

But this method gave me this error in logcat

2022-07-08 03:15:07.544 25513-25752/com.nightowl.stylo E/tag102: no protocol: 

But when I put hardcoded string (showed in below method) in url method parameter then it return bitmap without any error. and wallpaper also gets changed

 try {
                        URL url = new URL("https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800");
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());

                        myWallpaperManager.setBitmap(image,null,false,WallpaperManager.FLAG_SYSTEM);

                    } catch(IOException e) {
                        Log.e("tag102",e.getMessage());
                    }

But i want random string to set and get me bitmap from that. So how i can achieve that? I also try to encode url by

 try {
        encodedURL = URLEncoder.encode(image_url, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        Log.e("tag3",e.toString());
    }

I am using three kind of url in this project

  1. https://images.pexels.com/photos/6336035/pexels-photo-6336035.jpeg?auto=compress&cs=tinysrgb&fit=crop&h=1200&w=800
  2. https://cdn.pixabay.com/photo/2020/11/27/22/07/naruto-5783102_960_720.png
  3. https://images.unsplash.com/photo-1641414315243-196e7382c32d?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1178&q=80

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文