0

I have a string which consists an URL of the image, in the same activity I am viewing the image through URL. But to set the same image as my wallpaper, I am converting the string to Uri and then to Bitmap to use setBitmap.But I am still getting error telling No image was chosen.

Code is below: newString has the URL of the image.

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();
            }

        }
    });
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sourav Singh
  • 85
  • 1
  • 1
  • 12

1 Answers1

0

It seems like I can't comment. So i answer here.

I am not sure about your url being on internet or local. I don't find anything wrong with your code. So, my deduction is your onclicklistener is set before it can fetch image.(might need to use asyntask to save image) Are you displaying on your imageview from same image resource?

phonemyatt
  • 1,287
  • 17
  • 35