Android - how to set the wallpaper image
From this page on the developer site:
public void setStream (InputStream data)
Change the current system wallpaper to a specific byte stream. The give InputStream is copied into persistent storage and will now be used as the wallpaper. Currently it must be either a JPEG or PNG image.
Android set image as wallpaper
Try below code in ImagePagerActivity, i tested below code and it is working.
// fetch bitmap from view
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view
.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
// if we unable to get background drawable then we will set white color as wallpaper
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
public void setWall(int i) {
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
// below line of code will set your current visible pager item to wallpaper
// first we have to fetch bitmap from visible view and then we can pass it to wallpaper
myWallpaperManager.setBitmap(getBitmapFromView(pager.getChildAt(1)));
// below line of code will set input stream data directly to wallpaper
// myWallpaperManager.setStream(InputStream Data);
// below line of code will set any image which is in the drawable folder
// myWallpaperManager.setResource(R.drawable.icon);
} catch (IOException e) {
e.printStackTrace();
}
}
It will set current visible pager's item view(if it is progress wheel or image).
how to set wallpaper as lockscreen or both(home and lock)?
Check my code if its working for you :)
WallpaperManager myWallpaperManager;
private static final int CHOOSE_IMAGE = 22;
private Button btnSetWallpaper;
String[] options = new String[]{
"Home Screen",
"Lock Screen",
"Both"
};
In OnCreate()
btnSetWallpaper = findViewById(R.id.btnSetWallpaper);
btnSetWallpaper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "choose image"), CHOOSE_IMAGE);
}
});
In OnActivityResult()
if (requestCode == CHOOSE_IMAGE && data != null) {
mCropImageUri = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mCropImageUri);
Log.e(TAG, "onActivityResult: BIT " + bitmap);
} catch (IOException e) {
e.printStackTrace();
}
final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Choose from below");
final Bitmap finalBitmap = bitmap;
builder.setItems(options, new DialogInterface.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(DialogInterface dialogInterface, int i) {
String selectedItem = Arrays.asList(options).get(i);
if (selectedItem.equals(options[0])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_SYSTEM);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
} else if (selectedItem.equals(options[1])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
} else if (selectedItem.equals(options[2])) {
try {
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setBitmap(finalBitmap, null, false, WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM);
Toast.makeText(MainActivity.this, "Wallpaper set successfully!", Toast.LENGTH_SHORT).show();
dialog.dismiss();
} catch (Exception e) {
Log.e(TAG, "onResourceReady: " + e.getMessage());
}
}
}
});
dialog = builder.create();
dialog.show();
}
You wanna just set Flags for various Wallpaper Options.
Flags like...
WallpaperManager.FLAG_LOCK | WallpaperManager.FLAG_SYSTEM : for both Screen
WallpaperManager.FLAG_SYSTEM : for Home Screen
WallpaperManager.FLAG_LOCK :for Lock Screen
Thanks :)
How to set wallpaper from imageview
I've posted my code whatever i'm using in my application. I've also take the image from GridView
and, when anyone of the image is being selected. That'll set as wallpaper.
But, my code seems just different. I never used any ImageView
MainActivity.this
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
ImageAdapter i = (ImageAdapter)parent.getAdapter();
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),(int)i.getItemId(position));
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(MainActivity.this, "Wallpaper set", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(MainActivity.this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
}
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return mFullSizeIds[position];
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(300, 250));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.wallpaper1t,
R.drawable.wallpaper2t,
R.drawable.wallpaper3t,
R.drawable.wallpaper4t,
R.drawable.wallpaper5t,
R.drawable.wallpaper6t,
R.drawable.wallpaper7t,
R.drawable.wallpaper8t
};
private Integer[] mFullSizeIds = {
R.drawable.wallpaper1,
R.drawable.wallpaper2,
R.drawable.wallpaper3,
R.drawable.wallpaper4,
R.drawable.wallpaper5,
R.drawable.wallpaper6,
R.drawable.wallpaper7,
R.drawable.wallpaper8
};
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"/>
Maybe, this can be helps you lot.
Related Topics
Arrayadapter in Android to Create Simple Listview
How to Create a User-Defined Function in SQLite
Adding Button Action in Custom Notification
How to Get Current Activity's Layout and Views via Adb
How to Use Web Camera in Android Emulator to Capture a Live Image
How to Load External Webpage in Webview
Webview's HTML Button Click Detection in Activity
Get Text from Web Page to String
How to Make a Phone Call from HTML on Android
Letting Webview on Android Work with Prefers-Color-Scheme: Dark
How to Place Fragment Arrylist Scrollabletabsactivity
Read Only File System on Android
React-Native: Command Not Found
Export a Python Script to an Android Executable (.Apk) with Python-For-Android
Android Plugin Install Problems in Eclipse
What Is Exactly a "Clock Tick" in the Context of Android CPU Usage