You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

小米设备GalleryOpenProvider报错:重启App后读取Uri崩溃

我开发了一个包含ProfilePageFragment的Android应用,用户可在该Fragment中选择图库图片或使用相机拍摄照片,将图片Uri以字符串形式存入本地数据库并在Fragment中展示。代码在模拟器上运行正常,但在小米设备上销毁App后,从数据库读取Uri并尝试展示时应用崩溃,抛出com.miui.gallery.provider.GalleryOpenProvider相关错误,查阅资料后未找到解决方案。

相关代码如下:

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

    private lateinit var routineTasksViewModel: RoutineTasksViewModel

    private var userName : String? = null
    private var userProfilePhotoUri : String? = null

    private lateinit var cameraPermissions : Array<String>
    private lateinit var storagePermissions : Array<String>
    private var imageUri : Uri? = null

    private companion object {
        private const val CAMERA_REQUEST_CODE = 100
        private const val STORAGE_REQUEST_CODE = 101

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentProfilePageBinding.inflate(layoutInflater,container,false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        routineTasksViewModel = (activity as MainActivity).routineTasksViewModel

        cameraPermissions = arrayOf(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
        storagePermissions = arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE)

        getUserProfileInformations()


        binding.apply {
            profilePhotoIV.setOnClickListener{
                showInputImageDialog()

            }

        }

    }



    //popUp menu and item click funcs
    private fun showInputImageDialog() {
        val popUpMenu = PopupMenu(requireContext(),binding.profilePhotoIV)
        popUpMenu.menu.add(Menu.NONE,1,1, R.string.camera)
        popUpMenu.menu.add(Menu.NONE,2,2, R.string.gallery)

        popUpMenu.show()

        popUpMenu.setOnMenuItemClickListener { menuItem ->
            val id = menuItem.itemId
            if (id ==1){
                if (checkCameraPermission()){
                    pickImageCamera()
                }else{
                    requestCameraPermission()
                }

            }else if(id == 2){

                if (checkStoragePermission()){
                    pickImageGallery()
                }else{
                    requestStoragePermission()
                }
            }

            return@setOnMenuItemClickListener true
        }

    }

    //two funcs check the permissions
    private fun checkStoragePermission() : Boolean { //permissionGranted -> true
        return ContextCompat.checkSelfPermission(requireContext(),
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
    }

    private fun checkCameraPermission() : Boolean{ //permissionGranted -> true
        val cameraResult = ContextCompat.checkSelfPermission(requireContext(),
            Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
        val storageResult = ContextCompat.checkSelfPermission(requireContext(),
            Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
        return cameraResult && storageResult
    }
    //

    private fun pickImageGallery(){
        val intent = Intent(Intent.ACTION_PICK)
        intent.type = "image/*"

        galleryActivityResultLauncher.launch(intent)
    }

    private fun pickImageCamera(){
        val values = ContentValues()
        values.put(MediaStore.Images.Media.TITLE, "Sample Title")
        values.put(MediaStore.Images.Media.DESCRIPTION, "Sample Description")

        imageUri = activity?.contentResolver?.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)


        cameraActivityResultLauncher.launch(intent)

    }

    //launcher to camera
    private val cameraActivityResultLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result ->
            if ( result.resultCode == Activity.RESULT_OK){
                //image came from camera
                binding.profilePhotoIV.setImageURI(imageUri)
                updateProfilePhotoToDB(imageUri)
            }else{
                //Toast.makeText(requireContext(), R.string.permissions_required, Toast.LENGTH_SHORT).show()
            }
        }
    //launcher to gallery
    private val galleryActivityResultLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()){ result ->
            if ( result.resultCode == Activity.RESULT_OK){
                val data = result.data
                imageUri = data!!.data

                binding.profilePhotoIV.setImageURI(imageUri)
                updateProfilePhotoToDB(imageUri)

            }else{
                //Toast.makeText(requireContext(), R.string.permissions_required, Toast.LENGTH_SHORT).show()

            }
        }

    private fun requestStoragePermission() {
        ActivityCompat.requestPermissions(requireActivity(),storagePermissions, STORAGE_REQUEST_CODE)
    }

    private fun requestCameraPermission()  {
        ActivityCompat.requestPermissions(requireActivity(),cameraPermissions, CAMERA_REQUEST_CODE)

    }

    //when we request to take permission, according to request code pickImageCamera or pickImageGallery will work.
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        when (requestCode){
            CAMERA_REQUEST_CODE -> {
                if (grantResults.isNotEmpty()){
                    val cameraAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED
                    val storageAccepted = grantResults[1] == PackageManager.PERMISSION_GRANTED

                    if (cameraAccepted && storageAccepted){
                        pickImageCamera()
                    }
                }
            }

            STORAGE_REQUEST_CODE -> {
                if (grantResults.isNotEmpty()){
                    val storageAccepted = grantResults[0] == PackageManager.PERMISSION_GRANTED
                    if (storageAccepted){
                        pickImageGallery()
                    }
                }
            }
        }
    }

}

内容的提问来源于stack exchange,提问作者Ugur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 12:55:35