android - How to access Storage from SD card connected via OTG -
i try read , change textfile on external storage sd card. make example more easy inserted content uri file chooser.
i run app on nexus 5x android 7 (sdk 24). minsdkversion set 19 , targetsdkversion 23.
package de.cowabuja.androiduriaccess; import android.content.intent; import android.net.uri; import android.support.v7.app.appcompatactivity; import android.os.bundle; public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); string uristring = "content://com.android.externalstorage.documents/document/primary%3adcim%2fany%2dir%2fthefile.txt"; uri uri = uri.parse(uristring); getapplicationcontext().granturipermission(getpackagename(), uri, intent.flag_grant_read_uri_permission); } }
here log:
... e/androidruntime: fatal exception: main process: de.cowabuja.androiduriaccess, pid: 2972 java.lang.runtimeexception: unable start activity componentinfo{de.cowabuja.androiduriaccess/de.cowabuja.androiduriaccess.mainactivity}: java.lang.securityexception: uid 10132 not have permission uri 0 @ content://com.android.externalstorage.documents/document/primary%3adcim%2fany%2fdir%2fthefile.txt @ android.app.activitythread.performlaunchactivity(activitythread.java:2646) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2707) @ android.app.activitythread.-wrap12(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1460) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6077) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:865) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:755) caused by: java.lang.securityexception: uid 10132 not have permission uri 0 @ content://com.android.externalstorage.documents/document/primary%3adcim%2fany%2fdir%2thefile.txt @ android.os.parcel.readexception(parcel.java:1683) @ android.os.parcel.readexception(parcel.java:1636) @ android.app.activitymanagerproxy.granturipermission(activitymanagernative.java:4808) @ android.app.contextimpl.granturipermission(contextimpl.java:1655) @ android.content.contextwrapper.granturipermission(contextwrapper.java:712) @ de.cowabuja.androiduriaccess.mainactivity.oncreate(mainactivity.java:19) @ android.app.activity.performcreate(activity.java:6664) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1118) @ android.app.activitythread.performlaunchactivity(activitythread.java:2599) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2707) @ android.app.activitythread.-wrap12(activitythread.java) @ android.app.activitythread$h.handlemessage(activitythread.java:1460) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6077) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:865) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:755) ...
i try call granturipermission because try solve exception did before:
java.lang.securityexception: permission denial: reading de.cowabuja.androidaccess uri content://com.android.externalstorage.documents/document/primary%3adcim%2fany%2dir%2fthefile.txt pid=1399, uid=10350 requires android.permission.manage_documents, or granturipermission()
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.cowabuja.androiduriaccess"> <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsrtl="true" android:theme="@style/apptheme"> <activity android:name=".mainactivity"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <uses-permission android:name="android.permission.write_external_storage" /> <uses-permission android:name="android.permission.read_external_storage" /> </application> </manifest>
you cannot assemble uri
third-party provider , expect work. provider has not granted permission work content. @ most, previously gave temporary access content, rights died when process died.
either:
move file 1 of locations have filesystem access on removable storage:
getexternalfilesdirs()
,getexternalcachedirs()
, ,getexternalmediadirs()
. if methods (oncontext
) return 2+ entries, second , subsequent ones directories on removable storage can use. can use normal java file i/o (e.g.,file
,fileinputstream
).use the storage access framework ask user choose file work with. user may elect choose 1 on removable storage, or not, user's device , user's files. use
uri
contentresolver
, via methodsopeninputstream()
.
Comments
Post a Comment