Android Question Open External File not related to my App through intent in a 3rd party App [solved]

superkan

Member
Licensed User
intent code:
'Asking and getting rp.PERMISSION_WRITE_EXTERNAL_STORAGE is done, also confirmed in Settings/permission of device and checked through a process variable before executing the below code

    Dim properFileStr As String = "file://" & StrPath.trim
    Dim i As Intent 'Requires a reference to the Phone library

  
    i.Initialize(i.ACTION_VIEW, properFileStr)
  
    Dim Pos As Int = properFileStr.LastIndexOf(".")
    Dim Ext As String = properFileStr.SubString(Pos)

    'Type must be set after calling SetFileUriAsIntentData
    Starter.Provider.SetFileUriAsIntentData(i, properFileStr )

    Select Case Ext.ToUpperCase
        Case "JPG"
            i.SetType("image/*")
        Case "TXT"
            i.SetType("text/plain")
        Case "PNG"
            i.SetType("image/*")
    End Select

     Try
      StartActivity(i)
     Catch
        ToastMessageShow("couldn't open the file in default viewer", False)   'not executed in my case
        ToastMessageShow(properFileStr, True)
     End Try

I started by copying FileProvider example's fileprovider, starter, manifest code as is. When supplied path is "/storage/emulated/0/Download/myText.txt" choosing chrome as the target app reveals the final pointed path to be "content://my.packagename.provider/name/file%3A/storage/emulated/0/Download/myText.txt" which is shown by various apps (including chrome) that open through the chooser dialog as Requested file was not found. Keeping or removing file:// prepender code only keeps or removes that part of the final path - "file%3A" but, the behavior is same.

Code:
'. . .android:targetSdkVersion="26"
. . . .
'==================================File Provider ** Starts
AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
)

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,
   <files-path name="name" path="shared" />
)


Since nothing is found in the release mode logs, I need some expert advice here about how to tackle the problem. Am I missing something in the Manifest/Permission or is it the intent? Using FileProvider since the ordinary file:// based opening through intent throws the uri exposed error (sdk 26).
 
Last edited:

superkan

Member
Licensed User
Hi Erel. Sorry to disturb you. Just while trying to write this question clearly, I found the solution to it even though I was stuck for past 2-3 days. I will remove this thread.
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
Why remove the question? You can also put [Solved] in the title? When everyone has thrown away their solved question, there is little left for others to learn from.
 
Upvote 0

superkan

Member
Licensed User
First of all, the app is a script which consists of a service which opens a video file at a specified time. The video belongs to the user i.e DCIM located, not the app/asset. Also the user prefers VLC to play it - hence the title.

I didn't know about the forum rules that's why my suggestion. The problem was really silly. I copied some code where in the expression for extension was erroneous (substring(pos) instead of substring(pos+1)). My case statement for setting intent type failed and coincidentally, I got the uri exposed error don't know why (FileProvider is a solution for this) as I am on SDK 26, maybe because of the file:// prepending etc. However, when I catched the extension error through a toastmessage logging yesterday, I went back to the usual, old non-file provider based intent invocation. This worked perfectly and File Provider was not required anywhere!

B4X:
    Dim properFileStr As String = StrPath.trim          '/storage/emulated/0/DCIM/test.mp4
    
    Dim i As Intent 'Requires a reference to the Phone library
    
        
    i.Initialize(i.ACTION_VIEW, properFileStr)
        
    Dim Pos As Int = properFileStr.LastIndexOf(".")
    Dim Ext As String = properFileStr.SubString(Pos+1)
    
    Select Case Ext.ToUpperCase
        Case "MP4"
            i.SetType("video/*")
        Case "TXT"
            i.SetType("text/plain")
        End Select
    
     Try
      StartActivity(i)
     Catch
        ToastMessageShow("encountered error", False)
     End Try
 
Upvote 0

MicroDrie

Well-Known Member
Licensed User
I see that I'm not the only one with simple overlooked type errors ;)
 
Upvote 0
Top