首页 文章

无法使用wallpaperManager将位图设置为墙纸

提问于
浏览
0

我有一个字符串,其中包含图像的URL,在我通过URL查看图像的同一活动中 . 但是要设置与我的壁纸相同的图像,我将字符串转换为Uri,然后转换为Bitmap以使用 setBitmap . 但是我仍然收到错误告诉 No image was chosen .

代码如下: newString 具有图像的URL .

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

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

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

相关问题