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

            }

        });
4

4 回答 4

4

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

享受 =)

于 2014-07-11T21:44:52.637 回答
3

方法 wallpaperManager.setResource() 需要来自应用程序的资源 ID。示例:我有 ID 为“myImage”的 ImageView,然后调用该方法将如下所示wallpaperManager.setResource(R.id.myImage)。在您的情况下,您的身份证无效。

于 2014-07-11T20:34:33.993 回答
0

而不是wallpManager.setResource(0)你应该使用,wallpManager.setResource(R.drawable.yourimage)因为它期待 adrawable并且在你的应用程序中没有 id = 0。

在您的代码中

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

    }

因为newStr从来都不是数字,所以总是一个 url,所以NumberFormatException总是被捕获。因此, 的值newInt始终初始化为 0。因此出现错误。

于 2014-07-11T20:36:26.853 回答
0

我意识到我必须先下载图像才能将其设置为墙纸!感谢您的帮助 AndroidWarrior 和 Owl。当我得到下载代码时,我会发布它。猫头鹰向我展示了如何下载壁纸:

            //--- 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

...而且我还想出了如何下载ViewPager图像(我需要在我的应用程序的不同区域都这样做):

            //--- 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
于 2014-07-11T20:53:13.153 回答