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:
1
| <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
1
| getApplicationContext().setWallpaper(bmp); |
It should be surrounded with try-catch statements.
So now the final app looks like:
So now the final app looks like:
Finally the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
| 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); } }} |

