RSS

Android app Changing the wallpaper

27 Jun

Hello guys so I modified the earlier code(Camera app) to be able to change the wallpaper at the homescreen. I  should mention that you have to add permission for changing wallpaper:

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

Change it at the android manifest. You may also consider changing the orientation to portrait. The code for changing the wallpaper is

getApplicationContext().setWallpaper(bmp);

It should be surrounded with try-catch statements.
So now the final app looks like:


ps/ that isnt me in the photo.

Finally the code:

package com.learning.gilo;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class Camera extends Activity implements View.OnClickListener{

	Button wall,take;
	ImageView pic;
	Intent i;
	int cameraData = 0;
	Bitmap bmp;

	protected void onCreate(Bundle b){
		super.onCreate(b);
		setContentView(R.layout.camera);

		InputStream is = getResources().openRawResource(R.drawable.back_mao);
		bmp = BitmapFactory.decodeStream(is);
		wall = (Button) findViewById(R.id.bSet);
		take = (Button) findViewById(R.id.bTake);
		pic = (ImageView) findViewById(R.id.ivImage);

		wall.setOnClickListener(this);
		take.setOnClickListener(this);

	}

	public void onClick(View v){
		switch(v.getId()){
		case R.id.bSet:
			try {
			   getApplicationContext().setWallpaper(bmp);
                           Toast.makeText(getApplicationContext(), "This is a toast that will say \"Wallpaper set\"", Toast.LENGTH_LONG).show();
			} catch (IOException e) {
				e.printStackTrace();
			}
			break;
		case R.id.bTake:
			i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
			startActivityForResult(i,cameraData);
			break;

		}
}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(resultCode == RESULT_OK){
			Bundle extras = data.getExtras();
			bmp = (Bitmap) extras.get("data");
			pic.setImageBitmap(bmp);

		}

	}
}

Advertisements
 
1 Comment

Posted by on June 27, 2012 in Android, Android tutorials, Code

 

Tags:

One response to “Android app Changing the wallpaper

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: