无法从外部存储打开文件(出现各种错误)

发布于 2025-01-12 05:01:40 字数 2883 浏览 2 评论 0原文

我正在尝试制作一个应用程序来处理随机文件夹中文件中的一些数据(理想情况下)。现在,我很难使用带有硬编码路径的文件:

    class MainActivity : AppCompatActivity() {

    var fileDir = ""
    var data = mutableListOf<List<String>>()
    val toAddPermissions = mutableListOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.fetch_button)
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.type = "*/*"

        toAddPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        toAddPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)

        button.setOnClickListener {
            if(hasReadPermission() && hasWritePermission())
                fetch()
        }

        requestPermission()
    }

    private fun requestPermission(){
        ActivityCompat.requestPermissions(this, toAddPermissions.toTypedArray(), 0)
    }

    private fun hasReadPermission() =
        ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

    private fun hasWritePermission() =
        ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

    private fun fetch(){
        val file = File(*some string*)
        val bufferedReader = file.bufferedReader()
        //some code processing contents of the file
    }

根据某个字符串,我收到以下错误: getExternalFilesDir("/storage/emulated/0/Download/sample3.txt").toString() ->;打开失败:EISDIR(是目录) “/storage/emulated/0/Download/sample3.txt”->打开失败:EACCES(权限被拒绝)

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.matto.telemetry">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:requestLegacyExternalStorage="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Telemetry">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我浏览了 Kotlin 和 Java 中的各种帖子,但没有结果。您对如何解决这个问题有什么想法吗?

I'm trying to make an app that would process some data (ideally) from a file in a random folder. For now I struggle to use a file with a hardcoded path:

    class MainActivity : AppCompatActivity() {

    var fileDir = ""
    var data = mutableListOf<List<String>>()
    val toAddPermissions = mutableListOf<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button = findViewById<Button>(R.id.fetch_button)
        val intent = Intent(Intent.ACTION_GET_CONTENT)
        intent.type = "*/*"

        toAddPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE)
        toAddPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE)

        button.setOnClickListener {
            if(hasReadPermission() && hasWritePermission())
                fetch()
        }

        requestPermission()
    }

    private fun requestPermission(){
        ActivityCompat.requestPermissions(this, toAddPermissions.toTypedArray(), 0)
    }

    private fun hasReadPermission() =
        ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

    private fun hasWritePermission() =
        ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

    private fun fetch(){
        val file = File(*some string*)
        val bufferedReader = file.bufferedReader()
        //some code processing contents of the file
    }

where depending on some string i get these errors:
getExternalFilesDir("/storage/emulated/0/Download/sample3.txt").toString() -> open failed: EISDIR (Is a directory)
"/storage/emulated/0/Download/sample3.txt" -> open failed: EACCES (Permission denied)

Here's my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.matto.telemetry">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:requestLegacyExternalStorage="true"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Telemetry">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I went through various posts both in Kotlin and Java with no results. Do you have any ideas on how to fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文