[英]Manage External Storage Permission not clickable
So, I am creating an app, with the goal of turning text to pdf, and saving it to the external storage.所以,我正在创建一个应用程序,目标是将文本转换为 pdf,并将其保存到外部存储中。 After running the app with no errors, I encountered a weird bug, the "Manage External Storage Permission" button was not clickable.运行应用程序没有错误后,我遇到了一个奇怪的错误,“管理外部存储权限”按钮无法点击。 After reading the stack overflow posts, it seems like I have wrote the proper functions, but all the examples were in activities, so the problem may be there看了栈溢出的帖子,好像我写了正确的函数,但是所有的例子都在活动中,所以问题可能就在那里
Any help?有什么帮助吗?
class CreatePdfFragment : Fragment(R.layout.fragment_create_pdf) {
private lateinit var binding: FragmentCreatePdfBinding
private val STORAGE_PERMISSION_CODE: Int = 100
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentCreatePdfBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.btnToPdf.setOnClickListener {
if (checkPermission()) {
Log.d(TAG, "onViewCreated: Permission already granted, create folder")
savePdf()
} else {
Log.d(TAG, "onViewCreated: Permission was not granted, request")
requestPermission()
}
}
}
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<out String>,
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.isNotEmpty()) {
val write = grantResults[0] == PackageManager.PERMISSION_GRANTED
val read = grantResults[1] == PackageManager.PERMISSION_GRANTED
if (write && read) {
Log.d(TAG, "onRequestPermissionsResult: External Storage Permission granted")
savePdf()
} else {
Log.d(TAG, "onRequestPermissionsResult: External Storage Permission denied...")
toast("External Storage Permission denied...")
}
}
}
}
private fun savePdf() {
//create object of Document class
val mDoc = Document()
//pdf file name
val mFileName = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis())
//pdf file path
val mFilePath = Environment.getExternalStorageDirectory().toString() + "/" + mFileName +".pdf"
try {
//create instance of PdfWriter class
PdfWriter.getInstance(mDoc, FileOutputStream(mFilePath))
//open the document for writing
mDoc.open()
//get text from EditText i.e. textEt
val mText = binding.etPdfText.text.toString()
//add author of the document (metadata)
mDoc.addAuthor("Atif Pervaiz")
//add paragraph to the document
mDoc.add(Paragraph(mText))
//close document
mDoc.close()
//show file saved message with file name and path
toast("$mFileName.pdf\nis saved to\n$mFilePath")
}
catch (e: Exception){
//if anything goes wrong causing exception, get and show exception message
toast(e.message.toString())
}
}
private val storageActivityResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
Log.d(TAG, "storageActivityResultLauncher: ")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager()) {
Log.d(TAG, "storageActivityResultLauncher: ")
savePdf()
} else {
Log.d(TAG, "storageActivityResultLauncher: Manage External Storage Permission is denied...")
toast("Manage External Storage Permission is denied...")
}
} else {
}
}
private fun requestPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
try {
Log.d(TAG, "requestPermission: try")
val intent = Intent()
intent.action = Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
val uri = Uri.fromParts("package", this@CreatePdfFragment.requireActivity().packageName, null)
intent.data = uri
storageActivityResultLauncher.launch(intent)
} catch (e: Exception) {
Log.d(TAG, "requestPermission: ", e)
val intent = Intent()
intent.action = Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION
storageActivityResultLauncher.launch(intent)
}
} else {
ActivityCompat.requestPermissions(this@CreatePdfFragment.requireActivity(),
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE),
STORAGE_PERMISSION_CODE)
}
}
private fun checkPermission(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Environment.isExternalStorageManager()
} else {
val write = ContextCompat.checkSelfPermission(this@CreatePdfFragment.requireActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)
val read = ContextCompat.checkSelfPermission(this@CreatePdfFragment.requireActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)
write == PackageManager.PERMISSION_GRANTED && read == PackageManager.PERMISSION_GRANTED
}
}
private fun toast(message: String) {
Toast.makeText(this@CreatePdfFragment.requireActivity(), message, Toast.LENGTH_SHORT).show()
}
} }
Please ignore, the permission below was missing请忽略,下面的权限丢失了
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.