【问题标题】:Set background from gallery to all activity background将画廊背景设置为所有活动背景
【发布时间】:2023-04-11 00:28:01
【问题描述】:

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

这就是我所做的 1.我想要应用用户选择的壁纸的每个活动都有一个共享偏好和两个其他属性。这里是

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

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

1.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.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 背景。但我想要用户选择的相同图像作为所有其他活动的背景。我的应用程序中几乎没有选择图像让用户将其用作背景。以下是我的做法。

  1. 在 SettingsActivity (onCreate) 中

    getPrefs = this.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
    

然后当用户点击任意一张选中的图片时,图片就会被选为每个活动的壁纸。

2.在特定的imageView内部

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

3.放置在所有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));

同样,我如何使用用户选择的图像作为每个活动的背景。我尝试从 BitMap 转换为 Drawable 但仍然失败。 任何帮助都会有所帮助。 p.s 大部分代码来自多个网站。提前致谢。

【问题讨论】:

    标签: background imageview sharedpreferences background-image wallpaper


    【解决方案1】:

    您必须执行这些步骤

    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. 当新的活动打开时,使用此路径从这里获取图像并将其设置为背景。见下文

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

    【讨论】:

    • 无语。再次感谢@Sohail Zahid 先生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2011-06-17
    • 2020-12-13
    • 1970-01-01
    相关资源
    最近更新 更多