0

I'm trying to use WallpaperManager in a ViewPager. I've got a button which is supposed to set the current image in the ViewPager to wallpaper. My problem comes with the line of code wallpManager.setResource(newInt);... the integer it comes up with is always 0 (zero), and so the app crashes and LogCat says there's no Resource at ID #0x0. As a test to see if I'm getting the correct image URL I've done this:

String newStr = images[position];
CharSequence cs = newStr;
Toast.makeText(UILPager.this, cs, Toast.LENGTH_SHORT).show();

And the resulting Toast shows the correct image URL. I can't figure out how to convert the URL which is in the form of "http://www.example.com/image.jpg" to an Integer so that the WallpaperManager can use it. Here's what the whole button code looks like:

            wallp_BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                WallpaperManager wallpManager = WallpaperManager.getInstance(getApplicationContext());


                String newStr = images[position];
                int newInt = 0;
                try{
                    newInt = Integer.parseInt(newStr);
                } catch(NumberFormatException nfe) {

                }

                CharSequence cs = newStr;
                try {
                    wallpManager.setResource(newInt);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                Toast.makeText(UILPager.this, cs, Toast.LENGTH_SHORT).show();

            }

        });
kirktoon1882
  • 1,221
  • 5
  • 24
  • 40

4 Answers4

4

Set wallpaper from URL

    try {
        URL url = new URL("http://developer.android.com/assets/images/dac_logo.png");
        Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
        wallpaperManager.setBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Enjoy =)

0wl
  • 836
  • 8
  • 19
3

A method wallpaperManager.setResource() requires resource id from your application. Example: I've ImageView with id "myImage" then call the method will look like as wallpaperManager.setResource(R.id.myImage). In your case your id not valid.

0wl
  • 836
  • 8
  • 19
0

Instead of wallpManager.setResource(0) you should use the wallpManager.setResource(R.drawable.yourimage) since it's expecting a drawable and there is none in your app with id = 0.

In your code

String newStr = images[position];
int newInt = 0;
try{
    newInt = Integer.parseInt(newStr);
    } catch(NumberFormatException nfe) {

    }

Since newStr is never a number, always a url so NumberFormatException is caught always. Hence the value of newInt is always initialized to 0. Hence the error.

CodeWarrior
  • 5,026
  • 6
  • 30
  • 46
0

I realized that I have to download the image before I can set it as wallpaper! Thanks for your help AndroidWarrior and Owl. When I get the download code worked out, I'll post it. Owl showed me how to download the wallpaper:

            //--- Wallpaper button
        wallp_BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    vpURL = new URL(images[position]);
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                WallpaperManager wallpManager = WallpaperManager.getInstance(getApplicationContext());
                 try {
                        Bitmap bitmap = BitmapFactory.decodeStream(vpURL.openStream());
                        wallpManager.setBitmap(bitmap);
                        Toast.makeText(UILPager.this, "The wallpaper has been set!", Toast.LENGTH_LONG).show();
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }

        });
        //--- END Wallpaper button

... and I also figured out how to download the ViewPager image as well (I need to do both in different areas of my app):

            //--- Wallpaper button
        wallp_BTN.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                String vpURLStr = images[position];
                GetVPImageTask downloadVPImageTask = new GetVPImageTask();
                downloadVPImageTask.execute(new String[] { vpURLStr });
            }

        });
        //--- END Wallpaper button



//--- Download ViewPager Image AsyncTask

private class GetVPImageTask extends AsyncTask<String, Void, Bitmap> {
    ProgressDialog getVPImageDia;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        getVPImageDia = new ProgressDialog(UILNPPager.this);
        getVPImageDia.setMessage("Grabbing the image...");
        getVPImageDia.setIndeterminate(false);
        getVPImageDia.show();
    }



    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap map = null;
        for (String url : urls) {
            map = downloadImage(url);
        }
        return map;
    }

    // Sets the Bitmap returned by doInBackground
    @Override
    protected void onPostExecute(Bitmap result) {

        try {
            getVPImageDia.dismiss();
            getVPImageDia = null;
        } catch (Exception e) {
            // nothing
        }

        Toast.makeText(UILNPPager.this, "The image be Downloaded!", Toast.LENGTH_SHORT).show();
        //downloaded_iv.setImageBitmap(result);


    }

    // Creates Bitmap from InputStream and returns it
    private Bitmap downloadImage(String url) {
        Bitmap bitmap = null;
        InputStream stream = null;
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        bmOptions.inSampleSize = 1;

        try {
            stream = getHttpConnection(url);
            bitmap = BitmapFactory.
                    decodeStream(stream, null, bmOptions);
            stream.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return bitmap;
    }

    // Makes HttpURLConnection and returns InputStream
    private InputStream getHttpConnection(String urlString)
            throws IOException {
        InputStream stream = null;
        URL url = new URL(urlString);
        URLConnection connection = url.openConnection();

        try {
            HttpURLConnection httpConnection = (HttpURLConnection) connection;
            httpConnection.setRequestMethod("GET");
            httpConnection.connect();

            if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                stream = httpConnection.getInputStream();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return stream;
    }
}

//--- END Download ViewPager Image AsyncTask
kirktoon1882
  • 1,221
  • 5
  • 24
  • 40