使用wallpaperManager

时间:2017-11-18 19:13:34

标签: android bitmap android-wallpaper

我有一个字符串,其中包含图像的URL,在我通过URL查看图像的同一活动中。但是要设置与我的壁纸相同的图像,我将字符串转换为Uri,然后转换为Bitmap以使用 setBitmap 。但是我仍然会收到错误消息没有选择图像

代码如下: newString 包含图片的网址。

final String myUrlStr = newString;
    URL url;
    Uri uri=null;
    try {
        url = new URL(myUrlStr);
        uri = Uri.parse( url.toURI().toString() );
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    try {
        image = MediaStore.Images.Media.getBitmap(this.getContentResolver(),uri);
    } catch (IOException e) {
        e.printStackTrace();
    }
    setButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            WallpaperManager wallpaperManager=WallpaperManager.getInstance(getApplicationContext());

            try {
                // Set the image as wallpaper
                if(image!=null)
                    wallpaperManager.setBitmap(image);
                else
                    Toast.makeText(getApplicationContext(), "No image was chosen.", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });

1 个答案:

答案 0 :(得分:0)

好像我无法发表评论。所以我在这里回答。

我不确定您的网址是在互联网上还是本地网站上。我发现你的代码没有任何问题。所以,我的推论是你的onclicklistener在它可以获取图像之前被设置。(可能需要使用asyntask来保存图像)你是否在相同的图像资源上显示你的imageview?