简体   繁体   English

Android - 从图库应用程序获取图像?

[英]Android - Get an Image from the Gallery App?

I need to be able to get an image from the gallery when the user clicks on a button.当用户单击按钮时,我需要能够从图库中获取图像。

Since there are heaps of answers to this, my question is how can I do this, set the selected image to a RelativeLayout and this RelativeLayout is in another Class.由于对此有很多答案,我的问题是如何做到这一点,将所选图像设置为RelativeLayout并且此 RelativeLayout 在另一个类中。

My project is set out like this: I have my SettingsActivity which is where the button is, and my MainActivity where the wallpaper is located.我的项目是这样设置的:我的 SettingsActivity 是按钮所在的位置,我的 MainActivity 是墙纸所在的位置。 I need the image to set to the RelativeLayout when the user navigates back to the MainActivity.当用户导航回 MainActivity 时,我需要将图像设置为 RelativeLayout。

Code I have tried:我试过的代码:

SettingsActivity:设置活动:

changeWallpaper.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view1)
        {
            Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });
....
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && 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();

        editor.putBoolean(SETWALLPAPER, wallpaperSelection);
        editor.commit();
    }
}

MainActivity:主要活动:

isWallpaper = prefs.getBoolean(SETWALLPAPER, false);
...
if(isWallpaper == true)
    {
        Toast.makeText(MainActivity.this, "Setting BG", Toast.LENGTH_LONG).show();
        wallpaperView.setBackgroundDrawable(new BitmapDrawable(bitmap));
    }

Thanks谢谢

Looks like you are not setting your picture on selecting the picture but inside onCreate()看起来您不是在选择图片时设置图片,而是在 onCreate() 内部

Put your if(..){} inside the onActivityResult() method and see.将您的if(..){}放在onActivityResult()方法中并查看。

Let me know if it works.让我知道它是否有效。 Use else and try to see result.使用else并尝试查看结果。 See if the if code runs?若见,如果代码运行?

MainActivity do nothing is because the activity doesn't know what user selected MainActivity什么都不做是因为活动不知道用户选择了什么

on SettingActivity onActivityResult , you may store picturePath in SharedPreferences在 SettingActivity onActivityResult ,您可以将picturePath存储在SharedPreferences

on MainActivity onResume , try to get the path of image and convert it to Bitmap and set it as backgroud drawable在 MainActivity onResume ,尝试获取图像的路径并将其转换为 Bitmap 并将其设置为背景可绘制

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM