Set Wallpaper Using Resources Within Apk

Posted on

This representative alter from the postal service “Restore default build-in wallpaper“, to add together constituent to set wallpaper using resources within APK.


Create /res/raw/ folder inwards your Android Studio project. Refer: Android Studio error: Expected resources of type raw

Modify layout/activity_main.xml to add together push to “Load Wallpaper from from Resources”.

<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidwallpaper.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <Button         android:id="@+id/displaywp"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Display Wallpaper"/>     <Button         android:id="@+id/loadwp"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Load Wallpaper from Gallery"/>     <Button         android:id="@+id/loadwpres"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Load Wallpaper from from Resources"/>     <Button         android:id="@+id/restorewp"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Restore default Wallpaper"/>     <ImageView         android:id="@+id/image"         android:layout_width="match_parent"         android:layout_height="match_parent" /> </LinearLayout>  

MainActivity.java

package com.blogspot.android_er.androidwallpaper;  import android.app.Activity; import android.app.WallpaperManager; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast;  import java.io.FileNotFoundException; import java.io.IOException;  populace degree MainActivity extends AppCompatActivity {      ImageView imageWallpaper;     Button btnLoadWallpaper, btnDisplayWallpaper, btnRestoreWallpaper;     Button btnLoadWallpaperRes;      person static in conclusion int RQS_OPEN_IMAGE = 1;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          imageWallpaper = (ImageView)findViewById(R.id.image);          btnDisplayWallpaper = (Button)findViewById(R.id.displaywp);         btnDisplayWallpaper.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 displayWallpaper();             }         });          btnLoadWallpaper = (Button)findViewById(R.id.loadwp);         btnLoadWallpaper.setOnClickListener(btnLoadWallpaperOnClickListener);          btnRestoreWallpaper = (Button)findViewById(R.id.restorewp);         btnRestoreWallpaper.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 WallpaperManager myWallpaperManager =                         WallpaperManager.getInstance(getApplicationContext());                 endeavor {                     myWallpaperManager.clear();                 } grab (IOException e) {                     e.printStackTrace();                 }             }         });          btnLoadWallpaperRes = (Button)findViewById(R.id.loadwpres);         btnLoadWallpaperRes.setOnClickListener(new View.OnClickListener() {             @Override             populace void onClick(View v) {                 WallpaperManager myWallpaperManager =                         WallpaperManager.getInstance(getApplicationContext());                 endeavor {                     myWallpaperManager.setResource(R.raw.androidstudio);                 } grab (IOException e) {                     e.printStackTrace();                 }              }         });          displayWallpaper();      }      View.OnClickListener btnLoadWallpaperOnClickListener = novel View.OnClickListener() {          @Override         populace void onClick(View v) {             Intent intent = novel Intent();              if (Build.VERSION.SDK_INT >=                     Build.VERSION_CODES.KITKAT) {                 intent.setAction(Intent.ACTION_OPEN_DOCUMENT);             } else {                 intent.setAction(Intent.ACTION_GET_CONTENT);             }              intent.addCategory(Intent.CATEGORY_OPENABLE);              // gear upwards MIME type for icon             intent.setType("image/*");              startActivityForResult(intent, RQS_OPEN_IMAGE);         }     };      person void displayWallpaper(){         WallpaperManager myWallpaperManager =                 WallpaperManager.getInstance(getApplicationContext());         Drawable drawableWallpaper = myWallpaperManager.getDrawable();         imageWallpaper.setImageDrawable(drawableWallpaper);          Toast.makeText(this,                 drawableWallpaper.getMinimumWidth() + " x " + drawableWallpaper.getMinimumHeight()                         + "\n" +                         drawableWallpaper.getIntrinsicWidth() + " x " + drawableWallpaper.getIntrinsicHeight(),                 Toast.LENGTH_LONG).show();     }      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == Activity.RESULT_OK) {             if (requestCode == RQS_OPEN_IMAGE) {                 Uri dataUri = data.getData();                 Uri wallpaperUri = dataUri;                 Toast.makeText(this, wallpaperUri.toString(), Toast.LENGTH_LONG).show();                  Bitmap newOriginalBM= loadBitmap(dataUri);                 reloadWallpaper(newOriginalBM);             }         }     }      person Bitmap loadBitmap(Uri src) {          Bitmap bm = null;          endeavor {             bm = BitmapFactory.decodeStream(                     getBaseContext().getContentResolver().openInputStream(src));         } grab (FileNotFoundException e) {             e.printStackTrace();         }          supply bm;     }      person void reloadWallpaper(Bitmap bm){         if(bm != null){             WallpaperManager myWallpaperManager =                     WallpaperManager.getInstance(getApplicationContext());              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {                 if(myWallpaperManager.isWallpaperSupported()){                     endeavor {                         myWallpaperManager.setBitmap(bm);                     } grab (IOException e) {                         e.printStackTrace();                     }                 }else{                     Toast.makeText(MainActivity.this,                             "isWallpaperSupported() NOT SUPPORTED",                             Toast.LENGTH_LONG).show();                 }             }else{                 endeavor {                     myWallpaperManager.setBitmap(bm);                 } grab (IOException e) {                     e.printStackTrace();                 }             }         }else{             Toast.makeText(MainActivity.this, "bm == null", Toast.LENGTH_LONG).show();         }     } }  

uses-permission of “android.permission.SET_WALLPAPER” is needed.