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

Time:04-25

   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://media/external/images/media/24/ORIGINAL/NONE/2048773088
                binding.backgroundImage.setImageURI(wallpaper.toUri())
            }

        }
    }

}

I can get image uri i can use it in startForResult. I can also save the uri (//content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/24/ORIGINAL/NONE/2048773088). But when i try reuse it in onViewCreated, there is an error like this

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

I want to be able to use the selectedImageUri I got with startForResult again even if the application is closed and opened.

CodePudding user response:

An uri obtained using ACTION_GET_CONTENT cannot be used later.

Use ACTION_OPEN_DOCUMENT instead and take persistable uri permission at obtaining the uri.

Then save uri.toString() and reuse later.

You do not want to save the image file we suppose.

  • Related