1

URLから画像を表示し、ボタンのクリックで表示された画像を電話の壁紙として設定したい..現在、画像を表示していますが、ボタンを押して保存しても何も起こりません..ここに私が使用しているコードがあります..私が間違っていることを見つけてください..事前に感謝します

set = (Button) findViewById(R.id.abcd);

new DownloadImageTask((ImageView) findViewById(R.id.imageView1)).execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");

set.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        Thread th = new Thread(){
            public void run(){
            WallpaperManager image = WallpaperManager.getInstance(getApplicationContext());
            try{
                image.setBitmap(photo);
            }
            catch(Exception ex)
            {
                ex.printStackTrace();
            }
            }
        };
        th.start();
    }

});

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap photo = null;
        try {
InputStream in = new java.net.URL(urldisplay).openStream();
babes = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
        }
        return photo;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
} 
4

2 に答える 2

4

この許可を追加しましたか?

<uses-permission android:name="android.permission.SET_WALLPAPER" />
于 2012-11-15T10:43:19.530 に答える
0

ここに例があります

    private class InitialTask extends AsyncTask<String, Void, Void> {

    private final ProgressDialog dialog = new ProgressDialog(
            ImageActivity.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.show();

    }

    // automatically done on worker thread (separate from UI thread)
    protected Void doInBackground(final String... a) {

        try {
            Thread.sleep(100);
            try {
                int newpos = position;

                /*
                 * ImageId=Id[newpos];
                 * ImageID=Integer.parseInt(ImageId.toString()); saveimg=
                 * img[newpos];
                 */
                String url1 = imageUrls[newpos];
                // imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(),
                // ids[position]));
                try {
                    URL ulrn = new URL(url1);
                    HttpURLConnection con = (HttpURLConnection) ulrn
                            .openConnection();
                    InputStream is = con.getInputStream();
                    bmp = BitmapFactory.decodeStream(is);

                    int widthPx = getWindowManager().getDefaultDisplay()
                            .getWidth();
                    int heightPx = getWindowManager().getDefaultDisplay()
                            .getHeight();
                    bmp = Bitmap.createScaledBitmap(bmp, widthPx, heightPx,
                            true);

                } catch (Exception ex) {

                }
            } catch (Exception e) {
                int i = 0;
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    // can use UI thread here
    protected void onPostExecute(final Void unused) {

        if (null != bmp) {
            imageView = new ImageView(cxt);
            imageView.setImageBitmap(bmp);
            BitmapDrawable drawable = (BitmapDrawable) imageView
                    .getDrawable();
            Bitmap bitmap = drawable.getBitmap();
            try {
                WallpaperManager myWallpaperManager = WallpaperManager
                        .getInstance(getApplicationContext());
                myWallpaperManager.setBitmap(bitmap);
                Toast.makeText(ImagePagerActivity.this, "Wallpaper set",
                        Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(ImagePagerActivity.this,
                        "Error setting Wallpaper", Toast.LENGTH_SHORT)
                        .show();
            }

        }

        if (this.dialog.isShowing()) {
            this.dialog.dismiss();

        }

    }

}
于 2012-11-15T10:47:45.407 に答える