Android 使用 URI 设置壁纸

发布于 2024-12-03 06:04:24 字数 127 浏览 1 评论 0原文

我有一个来自媒体存储的 URI 指向图像。我想用这个 URI 设置壁纸。我尝试使用 BitmapFactory.decodeFile 但问题是如果它很大,则会耗尽内存。

我还有其他方法可以做到这一点吗?

谢谢

I have a URI from the mediastore pointing to a image. I would like to set the wallpaper with this URI. I tryed using BitmapFactory.decodeFile but the problem is if its big it runs out of memory.

Is there another way i can do this.

thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任谁 2024-12-10 06:04:24

这个问题有点老了,但是,嘿……

你可以用下面的代码来解决这个问题。但是,如果您将图像做得非常小,然后将其拉伸以适合非常大的图像,则会降低质量。话虽这么说,如果图像太大,您甚至无法加载它,而您只想将其用作壁纸,我认为您可以接受以下内容。

            // use if resizing the image
            BitmapFactory.Options options = new BitmapFactory.Options();

            // set to true to set image bounds
            options.inJustDecodeBounds = true; 

            // set to 2, 4, 6, etc to create a progressively smaller image
            options.inSampleSize = 2; 

            // set to false to prepare image for decoding
            options.inJustDecodeBounds = false; 

            bitmap = BitmapFactory.decodeStream(is, null, options);

This question is a bit old, but what the hey...

You can get around this problem with the following code. However, if you make the image very small, and then stretch it to fit something very big, you will loose quality. That being said, if the image is so huge you can't even load it, and you just want it to act as your wallpaper, I think you will be fine with the following.

            // use if resizing the image
            BitmapFactory.Options options = new BitmapFactory.Options();

            // set to true to set image bounds
            options.inJustDecodeBounds = true; 

            // set to 2, 4, 6, etc to create a progressively smaller image
            options.inSampleSize = 2; 

            // set to false to prepare image for decoding
            options.inJustDecodeBounds = false; 

            bitmap = BitmapFactory.decodeStream(is, null, options);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文