How to change image dynamically in drawable folder in Android studio?

85 views Asked by At

I have this snippet below:

            public void onReceive(Context context, Intent intent) {
                    try {
                        int random_int = (int)Math.floor(Math.random() * (max - min + 1) + min);
                        if(random_int == 0){
                            wallpaperManager.setResource(R.drawable.phonewallpaper1, WallpaperManager.FLAG_LOCK);
                        }else if(random_int == 1){
                            wallpaperManager.setResource(R.drawable.phonewallpaper2, WallpaperManager.FLAG_LOCK);
                        }else if(random_int == 2){
                            wallpaperManager.setResource(R.drawable.phonewallpaper3, WallpaperManager.FLAG_LOCK);
                        }else if(random_int == 3){
                            wallpaperManager.setResource(R.drawable.phonewallpaper4, WallpaperManager.FLAG_LOCK);
                        }

                        // set the wallpaper by calling the setResource function and
                        // passing the drawable file
                    } catch (IOException e) {
                        // here the errors can be logged instead of printStackTrace
                        e.printStackTrace();
                    }

This code is basically set randomly the screenlock wallpaper based on the static images (4 images) in drawable folder. If I put 4 new images in a folder of android device, for example under "/storage/emulated/0/downloads/contents/".

How can you change the images dynamically in drawable folder based on images under "/storage/emulated/0/downloads/contents/"?

Is it easier just to replace programmatically the images in drawable folder, or just point the code to the folder "/storage/emulated/0/downloads/contents/" instead?

I have google around how to do this but cannot really find anything that set the screenlock wallpaper from a device folder.

Thank you

2

There are 2 answers

0
user3063697 On

And image1.canRead() is not readable

File image1 = new File(Environment.getExternalStorageDirectory(), "/downloads/image1.jpg");
File image2 = new File(Environment.getExternalStorageDirectory(), "/downloads/image2.jpg");
if (image1.exists()) {
  Toast.makeText(getApplicationContext(), "EXIST", Toast.LENGTH_LONG).show();
}
if (image1.canRead()) {
  Toast.makeText(getApplicationContext(), "READABLE", Toast.LENGTH_LONG).show();
} else {
  Toast.makeText(getApplicationContext(), "NOT READABLE", Toast.LENGTH_LONG).show();
}
int min = 0;
int max = 1;
//final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
FileInputStream fis;

Bitmap myBitmap1 =  BitmapFactory.decodeFile(image1.getAbsolutePath());
Bitmap myBitmap2 =  BitmapFactory.decodeFile(image2.getAbsolutePath());
Toast.makeText(getApplicationContext(), (String)image1.getAbsolutePath(), Toast.LENGTH_LONG).show();
if (myBitmap1 == null) {
  Toast.makeText(getApplicationContext(), "NULL", Toast.LENGTH_LONG).show();
}
//  Toast.makeText(getActivity(), (String)image1.getAbsolutePath(),Toast.LENGTH_LONG).show();
@SuppressLint("ResourceType")
// on below line initializing web view with id.
BroadcastReceiver receiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    try {
      int random_int = (int)Math.floor(Math.random() * (max - min + 1) + min);
      if (random_int == 0) {
        wallpaperManager.setBitmap(myBitmap1,null, true, WallpaperManager.FLAG_LOCK);
      } else if (random_int == 1) {
        wallpaperManager.setBitmap(myBitmap2,null, true, WallpaperManager.FLAG_LOCK);
      }
8
user3063697 On

tested like this below , get the black image instead of the wallpaper.

        File image1 = new File(Environment.getExternalStorageDirectory(), "/downloads/image1.jpg");
    File image2 = new File(Environment.getExternalStorageDirectory(), "/downloads/image2.jpg");
    final WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
    Bitmap myBitmap1 =  BitmapFactory.decodeFile(image1.getAbsolutePath());
    Bitmap myBitmap2 =  BitmapFactory.decodeFile(image2.getAbsolutePath());
    @SuppressLint("ResourceType")
    // on below line initializing web view with id.
    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
                try {
                    int random_int = (int)Math.floor(Math.random() * (max - min + 1) + min);
                    if(random_int == 0){
                        wallpaperManager.setBitmap(myBitmap1,null, true, WallpaperManager.FLAG_LOCK);
                    }else if(random_int == 1){
                        wallpaperManager.setBitmap(myBitmap2,null, true, WallpaperManager.FLAG_LOCK);
                    }