Setting the home wallpaper

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Setting the home wallpaper

Postby Benkr0n » Fri Feb 05, 2010 7:07 pm

i have a button with intent to pick an image
this returns that image's URI

with that URI i ImageView it as my application background
then i want to use another button to set that picture as the wallpaper

this is my code for that button...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onClick(View view)
  2.  
  3.                 {
  4.  
  5.                        
  6.  
  7.                         if (imageUriString == null) {
  8.  
  9.                                         showMsg("Please Select An Image");
  10.  
  11.                         }
  12.  
  13.                         else {                         
  14.  
  15.                                         InputStream is = getContentResolver().openInputStream(imageUri);
  16.  
  17.                                         bgImage = BitmapFactory.decodeStream(is);
  18.  
  19.                                         Context context = this.getBaseContext();
  20.  
  21.                                         context.setWallpaper(bgImage);
  22.  
  23.                                                
  24.  
  25.                                 showMsg("Image Set As Wallpaper");
  26.  
  27.                         }
  28.  
  29.                 }
  30.  
  31.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


the code errors at getBaseContext();
so what am i doing wrong?
should i use setStream instead? or what, help please...
Benkr0n
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Feb 05, 2010 6:45 pm

Top

Postby BUKsAPPs » Fri Feb 05, 2010 7:10 pm

Try:
Context context = MyClass.this.getBaseContext();

Where MyClass is the name of your class.
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Postby Benkr0n » Fri Feb 05, 2010 7:14 pm

BUKsAPPs wrote:Try:
Context context = MyClass.this.getBaseContext();

Where MyClass is the name of your class.


this breaks everything else...
errors at:
getContentResolver().openInputStream(imageUri)
and
context.setWallpaper(bgImage)
Benkr0n
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Feb 05, 2010 6:45 pm

Postby BUKsAPPs » Fri Feb 05, 2010 7:22 pm

What are you 2 new errors?

What kind of class contains your onClickListener?

Does that new code properly get your Context?
--If not, you might need getContext() rather than getBaseContext()
BUKsAPPs
Claustrum Lite - Area capture game
R:P:S Academy - play Rock/Paper/Scissors online
BUKsAPPs
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Sat Jan 16, 2010 9:01 pm
Location: USA

Postby Benkr0n » Fri Feb 05, 2010 7:31 pm

at
getContentResolver().openInputStream(imageUri)

Unhandled exception type fileNotFoundException
quick fix: Surround with try/catch

at
context.setWallpaper(bgImage)

Unhandled exception type IOException
quick fix: Surround with try/catch

when i changed it to Context context = MyClass.this.getBaseContext(); this line no longer errors
Benkr0n
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Feb 05, 2010 6:45 pm

Postby Benkr0n » Fri Feb 05, 2010 7:56 pm

here is more of my code... i'm pretty sure that my only problem is in actually setting the wallpaper, i can't find any good sources on the WallpaperManager... what i've found is that people still use setWallpaper even though it says its depreciated.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.     @Override
  2.  
  3.     public void onCreate(Bundle savedInstanceState)
  4.  
  5.     {
  6.  
  7.        
  8.  
  9.         super.onCreate(savedInstanceState);
  10.  
  11.         setContentView(R.layout.main);
  12.  
  13.            
  14.  
  15.         Button sel = (Button) findViewById(R.id.ButtonSEL);
  16.  
  17.         sel.setOnClickListener(new View.OnClickListener()
  18.  
  19.         {
  20.  
  21.                 public void onClick(View view)
  22.  
  23.                 {
  24.  
  25.                        
  26.  
  27.                     Intent intent = new Intent(Intent.ACTION_PICK);
  28.  
  29.                     intent.setType("image/*");
  30.  
  31.                     startActivityForResult(intent, 2);
  32.  
  33.                            
  34.  
  35.                 }
  36.  
  37.         });
  38.  
  39.         Button set = (Button) findViewById(R.id.ButtonSET);
  40.  
  41.         set.setOnClickListener(new View.OnClickListener()
  42.  
  43.         {
  44.  
  45.                 public void onClick(View view)
  46.  
  47.                 {
  48.  
  49.                        
  50.  
  51.                         if (imageUriString == null) {
  52.  
  53.                                 showMsg("Please Select An Image");
  54.  
  55.                         }
  56.  
  57.                         else {
  58.  
  59.                                
  60.  
  61.                                         InputStream is = getContentResolver().openInputStream(imageUri);
  62.  
  63.                                         bgImage = BitmapFactory.decodeStream(is);
  64.  
  65.                                         Context context = MyClass.this.getBaseContext();
  66.  
  67.                                         context.setWallpaper(bgImage);
  68.  
  69.                                                
  70.  
  71.                                 showMsg("Image Set As Portrait Wallpaper");
  72.  
  73.                         }
  74.  
  75.                 }
  76.  
  77.         });
  78.  
  79.     }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


here is my onActivityResult.... no errors but in case you need it
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3.         public void onActivityResult(int requestCode, int resultCode, Intent data)
  4.  
  5.         {
  6.  
  7.                 super.onActivityResult(requestCode, resultCode, data);
  8.  
  9.  
  10.  
  11.                 if (resultCode == RESULT_OK)
  12.  
  13.                 {
  14.  
  15.  
  16.  
  17.                         if (requestCode == 2)
  18.  
  19.                         {
  20.  
  21.                                
  22.  
  23.                                 Uri imageUri = data.getData();
  24.  
  25.                                 Log.i("IMAGE SELECTED", "" + imageUri);
  26.  
  27.                                
  28.  
  29.                                 setImageUri(imageUri);
  30.  
  31.                                 setImageUriString("" + imageUri);
  32.  
  33.                                
  34.  
  35.                                 showMsg("Image Selected");
  36.  
  37.                                
  38.  
  39.                         iv = (ImageView) findViewById(R.id.previewbackground);
  40.  
  41.                         iv.setImageURI(imageUri);
  42.  
  43.                         iv.setScaleType(ScaleType.FIT_CENTER);
  44.  
  45.  
  46.  
  47.                                
  48.  
  49.                         }
  50.  
  51.                 }
  52.  
  53.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
Benkr0n
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Feb 05, 2010 6:45 pm

Top

Postby shishir.bobby » Thu Apr 01, 2010 10:54 am

u'll need a permission to set wallpaper.........
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby shishir.bobby » Thu Apr 01, 2010 10:55 am

u'll need a permission to set wallpaper.........
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Postby shishir.bobby » Thu Apr 01, 2010 12:38 pm

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


this u need to specify in androidmanifest...........
shishir.bobby
Experienced Developer
Experienced Developer
 
Posts: 76
Joined: Thu Apr 09, 2009 11:41 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests