final int RESULT_LOAD_IMAGE=100;
On button Click
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 100);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
String wallpaper = selectedWallpaperImage(getApplicationContext(),data);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
wallpaperManager.setBitmap(BitmapFactory.decodeFile(wallpaper));
//this line of code will set the selected image as your wallpaper...........
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
public static String selectedWallpaperImage(Context context,Intent data){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return picturePath;
}
and add permission <uses-permission android:name="android.permission.SET_WALLPAPER" />
And if you want direct set image from application resource
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
//set wallpaper picture from resource here
myWallpaperManager.setResource(R.drawable.andr_gp);
Toast.makeText(getApplicationContext(),"Success set as wallpaper",22).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error set as wallpaper",22).show();
}
On button Click
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, 100);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
String wallpaper = selectedWallpaperImage(getApplicationContext(),data);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
wallpaperManager.setBitmap(BitmapFactory.decodeFile(wallpaper));
//this line of code will set the selected image as your wallpaper...........
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}}
}
public static String selectedWallpaperImage(Context context,Intent data){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
return picturePath;
}
and add permission <uses-permission android:name="android.permission.SET_WALLPAPER" />
And if you want direct set image from application resource
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
//set wallpaper picture from resource here
myWallpaperManager.setResource(R.drawable.andr_gp);
Toast.makeText(getApplicationContext(),"Success set as wallpaper",22).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(),"Error set as wallpaper",22).show();
}
No comments:
Post a Comment
Comments