Search code examples
backgroundimageviewsharedpreferencesbackground-imagewallpaper

Set background from gallery to all activity background


Here's what I'm trying to do. I wanted the user to choose a picture from their gallery as set it as every activity background in the application.

Here's what I did 1.I have a Shared Preference and two other attributes in every activity that I want to apply the wallpaper user choose. Here is the

    private static final String PREF_NAME = "nextage_quiz";
    private static final int PRIVATE_MODE = 0;
    SharedPreferences getPrefs;

2.The action take place in settings activity where when the user clicks on the ImageView, it will launch the Gallery Image Selector. Here's the code for the ImageView, the method to start the Gallery Image Selector.

1.Inside the ImageView ClickListener

    private static int Load_Image_From_Gallery = 1;
    Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, Load_Image_From_Gallery);

2.The onActivityResult Code

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == Load_Image_From_Gallery && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageViewBackground5.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
}

As you can see I manage to set the selected image from the gallery as the ImageView background. But I wanted the same image that the user selected to be the background for all the other activity. I have few selection of image in my app to let the user to use it as their background. Here's how I did it.

  1. In SettingsActivity (onCreate)

    getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    

Then when the user click on any of the selected image, then the picture is selected as the wallpaper for every activity.

2.Inside the particular imageView

    getPrefs.edit().putInt("id", R.drawable.wallpaper2).apply();

3.The code which is placed in all the activity to change the wallpaper.

    getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);

    ImageView background= (ImageView) findViewById(R.id.background);
    if(getPrefs.getInt("id",0) != 0) 
       background.setBackgroundResource(getPrefs.getInt("id",0));

So again, how can I use the image user selected as the background for every activity. I've tried converting from BitMap to Drawable but still fails. Any help would help. p.s Most of the codes are from multiple websites. Thanks in Advance.


Solution

  • You have to do these steps

    1. Save the selected imagePath in SharedPreferences at onActivityResult() dont save the R.drawable.wallpaper2 in preferences.

      ...
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String picturePath = cursor.getString(columnIndex);
      
      // Here is addition to your code
      getPrefs.edit().putString("path", picturePath).apply();
      
      cursor.close();
      ImageViewBackground5.setImageBitmap(BitmapFactory.decodeFile(picturePath));
      
    2. When new Activity Open use this path to get image from here and set it as background. See below

      public class MainActivity extends AppCompatActivity {

          private Context context;
          private ImageView img;
          private SharedPreferences getPrefs;
          private static final String PREF_NAME = "nextage_quiz";
          private static final int PRIVATE_MODE = 0;
      
      
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              context = this;
      
              getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
              img = (ImageView) findViewById(R.id.background);
      
              if (!getPrefs.getString("path", "").equalsIgnoreCase("")) {
                  img.setImageBitmap(BitmapFactory.decodeFile(getPrefs.getString("path", "")));
              }
              //your other tasks below there
          }
      }