Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

安卓demo模型使用本地路径一启动就闪退 #369

Open
fanxiangyu399 opened this issue Dec 9, 2024 · 0 comments
Open

安卓demo模型使用本地路径一启动就闪退 #369

fanxiangyu399 opened this issue Dec 9, 2024 · 0 comments

Comments

@fanxiangyu399
Copy link

fanxiangyu399 commented Dec 9, 2024

经群里大佬提点尝试后确认是权限的问题,现给出以下解决方案:

  • 在 AndroidManifest.xml 添加以下权限
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  • 在MainActivity.kt的oncreate方法里,加载模型前添加动态申请权限代码
val PERMISSIONS_REQUEST_STORAGE = 1
// 检查 Android 版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    // 请求 MANAGE_EXTERNAL_STORAGE 权限
    if (!Environment.isExternalStorageManager()) {
        val intent: Intent = Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION)
        val uri = Uri.fromParts("package", packageName, null)
        intent.setData(uri)
        startActivity(intent)
    }
} else {
    // 针对 Android 10 及以下版本,申请 WRITE_EXTERNAL_STORAGE 权限
    if (ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
        ) != PackageManager.PERMISSION_GRANTED
    ) {
        ActivityCompat.requestPermissions(
            this,
            arrayOf<String>(Manifest.permission.WRITE_EXTERNAL_STORAGE),
            PERMISSIONS_REQUEST_STORAGE
        )
    }
}
  • 至此,问题解决。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant