Android Question Open files with intent

Pooya1

Active Member
Licensed User
Hi
I have problem in open files in android +6
I try use FileProvider but my folder is in root external folder (File.DirRootExternalDir/cache)
But i cannot find correct code in forum
My app is chat application that user can send any file to other user
And other user after download it into root/cache,it is necessary to open file from this folder with intent
Example if file is picture so open it in photo or others
Thanks
 

Pooya1

Active Member
Licensed User
No. Read the full thread more carefully. Specifically the linked post.
Sorry
I try follow steps
https://www.b4x.com/android/forum/t...apk-using-api-24-or-higher.88184/#post-558473

I use below manifest code
B4X:
'********************  Required for file provider *************************
AddApplicationText(
  <provider
  android:name="android.support.v4.content.FileProvider"
  android:authorities="$PACKAGE$.provider"
  android:exported="false"
  android:grantUriPermissions="true">
  <meta-data
  android:name="android.support.FILE_PROVIDER_PATHS"
  android:resource="@xml/provider_paths"/>
  </provider>
)
CreateResource(xml, provider_paths,
   <external-files-path name="name" path="" />
)
'***************************************************************

For permission and copy file use below code
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("f1")
    Dim rp As RuntimePermissions
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
   
End Sub
 
 Sub Activity_PermissionResult (Permission As String, Result As Boolean)
     File.MakeDir(File.DirRootExternal,"cache")
    File.Copy(File.DirAssets,"21.jpg",File.DirRootExternal & "/cache","21.jpg")
 End Sub

For open file use below code
B4X:
Sub Button1_Click
    Intent_OpenFile(File.DirRootExternal & "/cache","21.jpg")
End Sub

Sub Intent_OpenFile(Dir As String,Filename As String) As Boolean
  
   Try
       Dim ext As String
       ext = Filename.SubString(Filename.LastIndexOf(".")+1)
      
       Dim i As Intent
       i.Initialize(i.ACTION_VIEW,CreateFileProviderUri(Dir,Filename))
       i.SetComponent("android/com.android.internal.app.ResolverActivity")
      
       Dim types As Map
       types.Initialize

       types.Put("apk","application/vnd.android.package-archive")
       types.Put("au","audio/basic")
       types.Put("avi","video/msvideo, video/avi, video/x-msvideo")
       types.Put("bmp","image/bmp")
       types.Put("bz2","Application/x-bzip2")
       types.Put("css","text/css")
       types.Put("dtd","Application/xml-dtd")
       types.Put("doc","Application/msword")
       types.Put("docx","Application/vnd.openxmlformats-officedocument.wordprocessingml.document")
       types.Put("dotx","Application/vnd.openxmlformats-officedocument.wordprocessingml.template")
       types.Put("es","Application/ecmascript")
       types.Put("exe","Application/octet-stream")
       types.Put("gif","image/gif")
       types.Put("gz","Application/x-gzip")
       types.Put("hqx","Application/mac-binhex40")
       types.Put("html","text/html")
       types.Put("jar","Application/java-archive")
       types.Put("jpg","image/jpeg")
       types.Put("js","Application/x-javascript")
       types.Put("midi","audio/x-midi")
       types.Put("mp3","audio/mpeg")
       types.Put("mpeg","video/mpeg")
       types.Put("ogg","audio/vorbis, Application/ogg")
       types.Put("pdf","Application/pdf")
       types.Put("pl","Application/x-perl")
       types.Put("png","image/png")
       types.Put("potx","Application/vnd.openxmlformats-officedocument.presentationml.template")
       types.Put("ppsx","Application/vnd.openxmlformats-officedocument.presentationml.slideshow")
       types.Put("ppt","Application/vnd.ms-powerpointtd>")
       types.Put("pptx","Application/vnd.openxmlformats-officedocument.presentationml.presentation")
       types.Put("psv","Application/postscript")
       types.Put("qt","video/quicktime")
       types.Put("ra","audio/x-pn-realaudio, audio/vnd.rn-realaudio")
       types.Put("ram","audio/x-pn-realaudio, audio/vnd.rn-realaudio")
       types.Put("rdf","Application/rdf, Application/rdf+xml")
       types.Put("rtf","Application/rtf")
       types.Put("sgml","text/sgml")
       types.Put("sit","Application/x-stuffit")
       types.Put("sldx","Application/vnd.openxmlformats-officedocument.presentationml.slide")
       types.Put("svg","image/svg+xml")
       types.Put("swf","Application/x-shockwave-flash")
       types.Put("tar.gz","Application/x-tar")
       types.Put("tgz","Application/x-tar")
       types.Put("tiff","image/tiff")
       types.Put("tsv","text/TAB-separated-values")
       types.Put("txt","text/plain")
       types.Put("wav","audio/wav, audio/x-wav")
       types.Put("xlam","Application/vnd.ms-excel.addin.macroEnabled.12")
       types.Put("xls","Application/vnd.ms-excel")
       types.Put("xlsb","Application/vnd.ms-excel.sheet.binary.macroEnabled.12")
       types.Put("xlsx","Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
       types.Put("xltx","Application/vnd.openxmlformats-officedocument.spreadsheetml.template")
       types.Put("xml","Application/xml")
       types.Put("zip","Application/zip, Application/x-compressed-zip")
      
       If types.ContainsKey(ext) Then
           i.SetType(types.Get(ext))
       Else
           i.SetType("application/vnd.android.package-archive")
       End If
                
       StartActivity(i)
      
       Return True
   Catch
       Return False
   End Try
  
End Sub

The file is copied into folder but i cannot open it from this folder
My error is
Caused by: java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/cache/21.jpg

Again sorry i spent complete 1 day for this but i couldn't did it
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0
Top