Home > OS >  Could not load text file from external storage of Cottonlin
Could not load text file from external storage of Cottonlin

Time:08-22

I am developing an Android application in Kotlin.

An error occurs when trying to import a text file stored in an external storage.

I tried various methods on stack overflow, but couldn't solve it.

I've simplified the code and uploaded it, I hope you can see if there are any problems.

The error contents are as follows.

java.lang.IllegalArgumentException: File /storage/emulated/0/Download/test.txt contains a path separator

MainActivity.kt

class MainActivity : AppCompatActivity() {

    var permission_list = arrayOf(
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        val vBinding = ActivityMainBinding.inflate(layoutInflater)
        super.onCreate(savedInstanceState)
        setContentView(vBinding.root)
        PermissionCheck()
        val path = File(Environment.getExternalStorageDirectory().absolutePath "/Download/test.txt").toString()
        val fileReader = FileReader(path)
        val bufferedReader = BufferedReader(fileReader)
        bufferedReader.readLines().forEach() {
            Log.d("Test", it)
        }
    }

    fun PermissionCheck(){
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.M){
            return
        }
        for(permission : String in permission_list){
            var chk = checkCallingOrSelfPermission(permission)
            if(chk == PackageManager.PERMISSION_DENIED){
                requestPermissions(permission_list, 0)
                break
            }
        }
    }
}

AndroidManifest.xml

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


    <application
        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.QuestionBank">
        <activity
            android:name=".TestActivity"
            android:exported="false" />
        <activity
            android:name=".PrintActivity"
            android:exported="false" />
        <activity
            android:name=".InputActivity"
            android:exported="false" />
        <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>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

</manifest>

my text file path enter image description here

CodePudding user response:

A quick fix for that is to add this code in the AndroidManifest.xml file:

<manifest ... >
    <!-- This attribute is "false" by default on apps targeting Android Q. -->
    <application android:requestLegacyExternalStorage="true" ... >
     ...
    </application>
</manifest>

You can read more about it here: https://developer.android.com/training/data-storage/use-cases

  • Related