Android Question Open external files with your app in android

Vellad

Member
Licensed User
Longtime User
I want to open any txt file with my App automatically when ever I touch on that txt file.
A list of apps will appear whenever I touch on the txt file. I want to add my App also in to that list. But how?
 

Vellad

Member
Licensed User
Longtime User
Thanks Erel,
I tired the modified version of image sharing method, but little luck.
Something went wrong
B4X:
'In Manifest Editor

AddActivityText(Main,

<intent-filter>

   <action android:name="android.intent.action.PICK" />

   <category android:name="android.intent.category.DEFAULT" />

   <data android:mimeType="image/*" />

</intent-filter>)





'In main page

Private Sub IsRelevantIntent(in As Intent) As Boolean

   If in.IsInitialized And in <> OldIntent And in.Action = in.ACTION_PICK Then

     OldIntent = in

     Return True

   End If

   Return False

End Sub
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
Manifest (One file or multiple text-files. Search the www for other mime-types ):

B4X:
AddActivityText(Main,
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/*" />
</intent-filter>
<intent-filter>
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/*" />
</intent-filter>)

In Activity Resume

B4X:
Sub Activity_Resume
    Log(Activity.GetStartingIntent.ExtrasToString)
    If Activity.GetStartingIntent.Action="android.intent.action.MAIN" Then
        'app was started by the user (not by "sharing to") so there's no file(s) to get...
        Return
    End If

    'App was started via "share to..." with textfile(s)
    FileList=GetUrisFromStartingIntent(Activity.GetStartingIntent)
    If FileList.Size>0 Then
        For i=0 To FileList.Size-1
            Dim uri As String = FileList.Get(i)
     
            Try
                File.Copy("ContentDir",uri,File.DirInternal & "/somefolderifneeded" ,"FileName".txt") 'change to unique filename otherwise it's overwritten... 
               
            Catch
                Log(LastException)
            End Try
         
        Next
    End If
    
End Sub

Private Sub GetUrisFromStartingIntent(in As Intent) As List
    Log(in.Action)
    Log(in.ExtrasToString)
    
    Dim FileList As List
    FileList.Initialize
    If in.IsInitialized And in <> OldIntent Then
        If in.Action = "android.intent.action.SEND" Or in.Action= "android.intent.action.SEND_MULTIPLE" Then
            FileList= GetImageUrisFromExtras(in.ExtrasToString, in.action)
        End If
        OldIntent = in
    End If
    Return FileList
End Sub

Sub GetImageUrisFromExtras (Extras As String, Intent As String) As List
    Log(Extras)
    Dim FileList As List
    FileList.Initialize
    
    Dim JO As JavaObject = Activity.GetStartingIntent
    Select Intent
        Case "android.intent.action.SEND"
            Dim uri As String = JO.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
            FileList.Add(uri)
        Case "android.intent.action.SEND_MULTIPLE"
            Dim uris As List = JO.RunMethod("getParcelableArrayListExtra", Array("android.intent.extra.STREAM"))
            If uris.Size > 0 Then
                For i = 0 To uris.Size-1
                    FileList.Add(uris.Get(i))
                Next
            End If
    End Select
    
    Return FileList
End Sub
 
Upvote 0
Top