【问题标题】:Setting Bitmap as wallpaper将位图设置为墙纸
【发布时间】:2012-11-15 10:35:58
【问题描述】:

我想显示来自 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);
    }
} 

【问题讨论】:

  • 您在 logcat 中是否遇到任何错误?
  • 请在此处发布 logcat 输出。另请参阅 Android 代码样式指南,您违反了约 70% 的规则..

标签: android android-asynctask wallpaper bitmapfactory


【解决方案1】:

您是否添加了此权限?

<uses-permission android:name="android.permission.SET_WALLPAPER" />

【讨论】:

    【解决方案2】:

    这是一个例子

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多