将画廊的背景设置为所有 activity 背景

Set background from gallery to all activity background

这就是我想要做的。 我希望用户从他们的图库中选择一张图片,并将其设置为应用程序中的每个 activity 背景。

这是我做的 1.I 在每个 activity 中都有一个共享首选项和其他两个属性,我想应用用户选择的墙纸。这是

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

2.The 操作发生在设置 activity 中,当用户单击 ImageView 时,它将启动 Gallery Image Selector。下面是 ImageView 的代码,即启动 Gallery Image Selector 的方法。

1.I在 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 代码

    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));

    }
}

如您所见,我设法将从图库中选择的图像设置为 ImageView 背景。但是我想要用户选择的相同图像作为所有其他 activity 的背景。我在我的应用程序中几乎没有选择图像让用户将其用作他们的背景。这是我的做法。

  1. 在SettingsActivity(onCreate)中

    getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    

然后当用户点击任意一张选中的图片时,每张图片都会被选为墙纸activity。

2.Inside 特定的 imageView

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

3.The代码里面全部放着activity换壁纸。

    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));

那么,我怎样才能将用户选择的图片用作每个 activity 的背景呢?我试过从 BitMap 转换为 Drawable 但仍然失败。 任何帮助都会有所帮助。 p.s 大部分代码来自多个网站。提前致谢。

您必须执行这些步骤

  1. 将选择的 imagePath 保存在 SharedPreferencesonActivityResult() 不要将 R.drawable.wallpaper2 保存在 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. 当新 Activity 打开时使用此路径从此处获取图像并将其设置为背景。见下文

    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
        }
    }