I am making a app to read a text file from an external storage through Kotlin. Even after adding permission tags and changing options in the manifest file, the following error is displayed and the text file cannot be imported. How can I solve this?
errCode:
java.lang.IllegalArgumentException: File content://com.android.externalstorage.documents/document/primary:Documents/test.txt contains a path separator
kotlin File
class MainActivity : AppCompatActivity() {
val getTextCode:Int = 2
override fun onCreate(savedInstanceState: Bundle?) {
val vBinding = ActivityMainBinding.inflate(layoutInflater)
super.onCreate(savedInstanceState)
setContentView(vBinding.root)
vBinding.getFromTxt.setOnClickListener {
val downUri = Uri.parse("/storage/self/primary/Documents")
openFile(downUri)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == getTextCode && resultCode == Activity.RESULT_OK) {
try{
data?.data?.also { uri ->
Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show()
Log.d("test", uri.toString())
openFileInput(uri.toString()).bufferedReader().useLines{ lines->
Log.d("test", lines.toString())
}
}
}catch(err:Exception){
Log.d("test", err.toString())
}
}
}
private fun openFile(pickerInitialUri: Uri){
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
type = "text/*"
putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri)
}
startActivityForResult(intent, getTextCode)
}
}
manifest File
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.questionbank">
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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"
android:requestLegacyExternalStorage="true"
>
<activity
android:name=".EditActivity"
android:exported="false" />
<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>
</manifest>
CodePudding user response:
openFileInput(uri.toString()
The method openFileInput() only expects a file name. Not a file path or a complete content scheme. And it would search for a file with that name in getFilesDir().
If you want to read the file using the uri then open an inputstream for the uri and read from the stream. In Java:
InputStream is = getContentResolver().openInputStream(uri);
Further you do not need any permission to do so. Nor do you have to change options whatever you mean by that.