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 回答