سؤال

I am creating wallpaper application and I am stuck at one litle problem. I've made application with image view and button to set as wallpaper. But there is a problem. When I open the picture and click on Set as Wallpaper button I want it to get ID from OPENED picture and set that picture as wallaper. here is my code

public class FullImageActivity extends Activity {
int toPhone;
ImageAdapter display;


@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]);

    Button buttonSetWallpaper = (Button)findViewById(R.id.setwallpaper);




       buttonSetWallpaper.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            WallpaperManager myWallpaperManager
             = WallpaperManager.getInstance(getApplicationContext());
            try {
             myWallpaperManager.setResource(R.drawable.pic_1);
            } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }

        }
    });
}
هل كانت مفيدة؟

المحلول

I think you should get the bitmap from imageview and use that bitmap to set wallpaper.Like this:--

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

Button buttonSetWallpaper = (Button)findViewById(R.id.setwallpaper);
   buttonSetWallpaper.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        WallpaperManager myWallpaperManager
         = WallpaperManager.getInstance(getApplicationContext());
        try {
         Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
         if(bitmap!=null)
         myWallpaperManager.setBitmap(bitmap);
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }

    }
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top