public class MainActivity extends AppCompatActivity
{
int hasPermissionSetAsWallpaper; // for set wallpaper permission.
final private int REQUEST_CODE_ASK_PERMISSIONS_SETASWALLPAPER = 201; // for external storage permission
@Override
protected void onCreate(Bundle savedInstanceState) {
........
if (Build.VERSION.SDK_INT >= 23)
{
hasPermissionReadExtSt = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE);
// READ_EXTERNAL_STORAGE izni var mı diye bak, yoksa gir.
if (hasPermissionReadExtSt != PackageManager.PERMISSION_GRANTED)
{
// shouldShowRequestPermissionRationale() method'u kullanıcı never ask'ı seçm,ş mi diye bakar.
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
// getErrorDialog() method'u kullanıcıdan tekrar izin vermesini ister.
getErrorDialog("You need to allow Read External Storage permission." +
"\nIf you disable this permission, You will not able to add attachment.", MainActivity.this, false).show();
}
else {
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE_ASK_PERMISSIONS_EXTERNAL_STORAGE);
}
}
hasPermissionSetAsWallpaper = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SET_WALLPAPER);
// SET_WALLPAPER izni var mı diye bak, yoksa gir.
if (hasPermissionSetAsWallpaper != PackageManager.PERMISSION_GRANTED)
{
// shouldShowRequestPermissionRationale() method'u kullanıcı never ask'ı seçmiş mi diye bakar.
if (shouldShowRequestPermissionRationale(Manifest.permission.SET_WALLPAPER)) {
// getErrorDialog() method'u kullanıcıdan tekrar izin vermesini ister.
getErrorDialog("You need to allow permission to be able to set an image as wallpaper" +
"\nIf you disable this permission, You cannot your drawings as wallpaper", MainActivity.this,).show();
}
else {
requestPermissions(new String[]{Manifest.permission.SET_WALLPAPER}, REQUEST_CODE_ASK_PERMISSIONS_SETASWALLPAPER);
}
}
}
...........
}
public AlertDialog.Builder getErrorDialog(String message, Context context, final boolean isFromsetwallpaper) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle(getString(R.string.app_name)).setMessage(message);
alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
if (Build.VERSION.SDK_INT >= 23) {
requestPermissions(new String[]{Manifest.permission.SET_WALLPAPER},
REQUEST_CODE_ASK_PERMISSIONS_SETASWALLPAPER);
}
}
});
return alertDialog;
}
public void setAsWallpaper(Context mContext, String imageName, int version){
public void setAsWallpaper(Context mContext, String imageName, int version){
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(mContext); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && !myWallpaperManager.isSetWallpaperAllowed()) // api>24 ise ve izin yoksa { myDialog.dismiss(); final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); alertDialog.setTitle("HATA!").setMessage("Wallpaper degistirme iznini vermediginiz icin bu ozelligi kullanamiyorsunuz."); alertDialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //do nothing. } }); return; } try { DrawingView.drawSavedThings(mContext,imageName,version); File fileImage = new File(Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES).toString() + File.separator + "MandalaApp" + File.separator + imageName + ".jpeg"); BitmapFactory.Options bmOptions = new BitmapFactory.Options(); Bitmap bitmapAsWallpaper = BitmapFactory.decodeFile(fileImage.getAbsolutePath(),bmOptions); int[] arr = MainActivity.getDeviceDimens((AppCompatActivity) mContext); bitmapAsWallpaper = Bitmap.createScaledBitmap(bitmapAsWallpaper,arr[0],arr[1],true); myWallpaperManager.setBitmap(bitmapAsWallpaper); bitmapAsWallpaper.recycle(); } catch (IOException e) { e.printStackTrace(); } }
}
also add permission to manifestx.ml for older android versions:
<uses-permission android:name="android.permission.SET_WALLPAPER"/>