简体   繁体   中英

set background from imageview on button click - Android

I have an Imageview and a Button in my project, i want the button to set the image as homescreen background but when i press the button, it doesn't do anything, no errors or force closes,

Thanks

package com.example.androidhive;

import java.io.IOException;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

 public class FullImageActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_image);

    // get intent data
    Intent i = getIntent();

    // Selected image id
    int position = i.getExtras().getInt("id");
    ImageAdapter imageAdapter = new ImageAdapter(this);

    ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
    imageView.setImageResource(imageAdapter.mThumbIds[position]);
    }


 public void setaswall(View view) { // SET AS WALLPAPER BUTTON
     WallpaperManager myWallpaperManager 
     = WallpaperManager.getInstance(getApplicationContext());
     };
  }

Get WallpaperManager by using

WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());

To get imageView bitmap and set as wallpaper, use

Bitmap bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
wallpaperManager.setBitmap(bitmap);

Or if you have the image resource id (as in your code example) then you can directly use

wallpaperManager.setResource(imageAdapter.mThumbIds[position]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM