You have changed your wall paper many times but have you think how that application do all this for you. So today in this tutorial I will cover how you can select wallpaper from gallery and then set it as your home screen wall paper.
In this tutorial I am taking picture from gallery and then you can select as your wall paper.
How to get image from gallery in Image View ? OR
How to Open Gallery for image Selection ?
To make it easy just go through full tutorial and you will also add this feature in your app.
How to get image from gallery in Image View ? OR
How to Open Gallery for image Selection ?
To make it easy just go through full tutorial and you will also add this feature in your app.
You may like these tutorials
Steps
- Create New Application and give name it what ever you like and Do next.
- minSdkVersion="17”
- targetSdkVersion="17".
- Give activity name
- Go to xml file and paste the below code.
activity_setwallpaper_demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="?attr/actionBarSize" tools:context="com.androprogrammer.tutorials.samples.SetWallpaperDemo"> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:scaleType="center" android:padding="@dimen/padding_normal" android:src="@mipmap/ic_launcher" android:layout_centerHorizontal="true" android:layout_marginTop="10dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="@dimen/padding_normal" android:layout_alignParentBottom="true"> <Button android:id="@+id/bt_imagepicker" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/str_opengallary"/> <Button android:id="@+id/bt_setwallpaper" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/str_setwallpaper"/> </LinearLayout> </RelativeLayout>
Above layout file consist of 2 Buttons and Image View so just copy above code and paste in your project xml(Activity layout) file.Now Copy Below code and Paste in your java file.
SetWallpaperDemo.java
package com.androprogrammer.tutorials.samples; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.provider.MediaStore; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.Window; import android.widget.Button; import android.widget.ImageView; import com.androprogrammer.tutorials.R; import com.androprogrammer.tutorials.activities.Baseactivity; import java.io.IOException; public class SetWallpaperDemo extends Baseactivity implements View.OnClickListener { protected View view; protected static final int SELECT_PICTURE = 1; protected String SelectedimagePath; protected Button bt1,bt2; protected ImageView iv1; protected Bitmap bmp; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setReference(); setToolbarElevation(7); setToolbarSubTittle(this.getClass().getSimpleName()); getSupportActionBar().setDisplayHomeAsUpEnabled(true); } @Override public void setReference() { view = LayoutInflater.from(this).inflate(R.layout.activity_setwallpaper_demo,container); bt1 = (Button) view.findViewById(R.id.bt_imagepicker); bt2 = (Button) view.findViewById(R.id.bt_setwallpaper); iv1 = (ImageView) view.findViewById(R.id.imageView1); bt1.setOnClickListener(this); bt2.setOnClickListener(this); } private String getPath(Uri getImageuri) { if(getImageuri == null) { return null; } String [] Projection = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(getImageuri, Projection, null, null, null); if(cursor != null) { int c_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); cursor.getString(c_index); cursor.close(); } return getImageuri.getPath(); } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: finish(); // NavUtils.navigateUpFromSameTask(this); // return true; break; } return super.onOptionsItemSelected(item); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_imagepicker: Intent gallery = new Intent(); gallery.setType("image/*"); gallery.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(gallery, "Select Picture"),SELECT_PICTURE); break; case R.id.bt_setwallpaper: try { getApplicationContext().setWallpaper(bmp); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (resultCode == RESULT_OK) { if (requestCode == SELECT_PICTURE) { Uri getImageuri = data.getData(); SelectedimagePath = getPath(getImageuri); if (bmp != null && bmp.isRecycled()) { bmp = null; } bmp = BitmapFactory.decodeFile(SelectedimagePath); iv1.setImageBitmap(bmp); } } } }
Well see above code and you can see I have used in built media provider for getting image path(absolute path). in order to change wall paper you have to add following permission in your application.
Screen Shot
So that’s it now run your app and make sure in emulator you have no images so run it in your own device or any other emulator. I have done it any checked also. But if you have any question or query ask in below comment box.
Keep coding..
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Screen Shot
So that’s it now run your app and make sure in emulator you have no images so run it in your own device or any other emulator. I have done it any checked also. But if you have any question or query ask in below comment box.
Keep coding..
0 comments :
Post a Comment