I'm doing a project to read Excel files from my phone and output the data to the screen.
This is how I try to do it:
private fun readDataExel() {
val file = Uri.parse("content://com.android.providers.media.documents/document/document%3A1000007061")
val inputStream = getContentResolver().openInputStream(file)
//Instantiate Excel workbook using existing file:
val workbook = WorkbookFactory.create(inputStream)
//Get reference to first sheet:
val sheet = workbook.getSheet("list")
for (row in sheet) {
if (row.getCell(0) == null || row.getCell(0).cellType == CellType.BLANK) {
break
}
val codeValue = row.getCell(0).stringCellValue.toString()
val liteCodeValue = codeValue.replace(".", "").replace("-", "")
val articleValue = row.getCell(1).stringCellValue.toString()
articles.add(ArticleModel(
code = codeValue,
liteCode = liteCodeValue,
title = articleValue
))
}
I get an error when I try to convert a uri to an Inputstream.
error in my logcat
FATAL EXCEPTION: main
Process: com.scan.scanforphonekarcher, PID: 13375java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.scan.scanforphonekarcher/com.scan.scanforphonekarcher.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{dd2bdda 13375:com.scan.scanforphonekarcher/u0a477} (pid=13375, uid=10477) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4325)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8762)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:604)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{dd2bdda 13375:com.scan.scanforphonekarcher/u0a477} (pid=13375, uid=10477) requires that you obtain access using ACTION_OPEN_DOCUMENT or related APIs
at android.os.Parcel.createExceptionOrNull(Parcel.java:3023)
at android.os.Parcel.createException(Parcel.java:3007)
at android.os.Parcel.readException(Parcel.java:2990)
at android.os.Parcel.readException(Parcel.java:2932)
at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:6136)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:7867)
...
I'm trying to get an Ecxel file (.xlsx) from my phone and read the data from the file.
I've Googled a lot and watched youtube videos, but I haven't found a solution to my problem.
Thanks for the help