0

で使用しようとしてWallpaperManagerViewPagerます。ViewPager の現在の画像を壁紙に設定するボタンがあります。私の問題はコード行にありますwallpManager.setResource(newInt);... 表示される整数は常に 0 (ゼロ) であるため、アプリがクラッシュし、LogCat は ID #0x0 にリソースがないと言います。正しい画像 URL を取得しているかどうかを確認するためのテストとして、次のようにしました。

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

結果のトーストには正しい画像 URL が表示されます。「 http://www.example.com/image.jpg」の形式の URL を整数に変換して WallpaperManager が使用できるようにする方法がわかりません。ボタンのコード全体は次のようになります。

            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

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 に答える
0

が期待されており、id = 0 のアプリには何もないため、代わりにwallpManager.setResource(0)を使用する必要があります。wallpManager.setResource(R.drawable.yourimage)drawable

あなたのコードで

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 に答える