如何保存和重用我从画廊获得的 uri?

How to save and reusing the uri i got from gallery?

   class WallpaperFragment : Fragment() {
    private var _binding: FragmentWallpaperBinding? = null
    private val binding get() = _binding!!

    private lateinit var SettingsDataStore: SettingsDataStore
    private var wallpaper = "default"

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentWallpaperBinding.inflate(inflater, container, false)

        /* PICK PHOTO and Bind It------------------------------------------------------------------*/
        var selectedImageUri: Uri? = null
        val startForResult =
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result: ActivityResult ->
                if (result.resultCode == Activity.RESULT_OK) {
                    val intent = result.data
                    selectedImageUri = intent?.data
                    binding.backgroundImage.setImageURI(selectedImageUri)
                    lifecycleScope.launch {
                        SettingsDataStore.saveLayoutToPreferencesStore(wallpaper, requireContext())
                    }
                }
            }

        binding.changeWallpaper.setOnClickListener() {
            val intent = Intent(Intent.ACTION_GET_CONTENT,MediaStore.Images.Media.EXTERNAL_CONTENT_URI )
            startForResult.launch(Intent.createChooser(intent, "Complete action using"))
        } /*----------------------------------------------------------------------------------------*/
        return binding.root

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        SettingsDataStore = SettingsDataStore(requireContext())
        SettingsDataStore.preferenceFlow.asLiveData().observe(viewLifecycleOwner) { value ->
            wallpaper = value
            if (wallpaper.isNotBlank() && wallpaper!="default"){
                Log.d("sss", wallpaper) //content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F24/ORIGINAL/NONE/2048773088
                binding.backgroundImage.setImageURI(wallpaper.toUri())
            }

        }
    }

}

我可以获得图像 uri 我可以在 startForResult 中使用它。我还可以保存 uri (//content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F24/ORIGINAL/NONE/2048773088)。 但是当我尝试在 onViewCreated 中重用它时,会出现这样的错误

java.lang.SecurityException: Permission Denial: opening provider com.google.android.apps.photos.contentprovider.impl.MediaContentProvider from ProcessRecord{677aefb 15300:com.yt.graduation/u0a149} (pid=15300, uid=10149) that is not exported from UID 10135

我希望能够再次使用通过 startForResult 获得的 selectedImageUri,即使应用程序关闭并打开也是如此。

使用ACTION_GET_CONTENT获取的uri以后不能使用。

改用 ACTION_OPEN_DOCUMENT 并在获取 uri 时获得持久性 uri 许可。

然后保存 uri.toString() 并稍后重用。

您不想保存我们假设的图像文件。