I am developing 2 apps:
App #1 will contain some bittorrent .torrent files in it's DirInternal.
App #2 is a bittorrent client that needs to open one or more of the other app's .torrent files.
In app #1 i'm creating an Intent with the Uri of the .torrent file as it's data and then broadcasting this Intent.
App #2 receives the broadcast Intent and now i need to open the .torrent file.
This is the Service in app #2 that receives the broadcast:
The log shows:
FYI app 1 is a 'file provider', it's manifest contains:
And the code used to create and broadcast the Intent:
(It's just a modified version of the FileProvider example).
How can app #2 open the .torrent file in app #1's DirInternal?
Stripping "file://" from the uri string value and trying to access "/data/data/b4a.example.deleteme/files/torrent-files/myfile.torrent" fails with a permission error (as to be expected).
App #1 will contain some bittorrent .torrent files in it's DirInternal.
App #2 is a bittorrent client that needs to open one or more of the other app's .torrent files.
In app #1 i'm creating an Intent with the Uri of the .torrent file as it's data and then broadcasting this Intent.
App #2 receives the broadcast Intent and now i need to open the .torrent file.
This is the Service in app #2 that receives the broadcast:
B4X:
Sub Service_Start (StartingIntent As Intent)
If StartingIntent.Action=ACTION_DOWNLOAD_TORRENT Then
Log("broadcast action found")
Dim JavaObject1 As JavaObject = StartingIntent
Dim Data As Object=JavaObject1.RunMethod("getData", Null)
Log("Data="&Data)
Dim FileUri As Uri=JavaObject1.RunMethod("getData", Null)
Log("FileUri="&FileUri)
Else
Log("broadcast action not found")
End If
End Sub
The log shows:
broadcast action found
Data=file:///data/data/b4a.example.deleteme/files/torrent-files/myfile.torrent
FileUri=(StringUri) file:///data/data/b4a.example.deleteme/files/torrent-files/myfile.torrent
FYI app 1 is a 'file provider', it's manifest contains:
B4X:
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="torrent-files" />
)
B4X:
'Returns the file uri.
Sub GetFileUri (Dir As String, FileName As String) As Object
If UseFileProvider = False Then
Dim uri As JavaObject
Return uri.InitializeStatic("android.net.Uri").RunMethod("parse", Array("file://" & File.Combine(Dir, FileName)))
Else
Dim f As JavaObject
f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
Dim fp As JavaObject
Dim context As JavaObject
context.InitializeContext
fp.InitializeStatic("android.support.v4.content.FileProvider")
Return fp.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End If
End Sub
Sub Go
Dim Phone1 As Phone
If Phone1.SdkVersion >= 24 Then
UseFileProvider = True
Else
UseFileProvider = False
End If
Log($"Using FileProvider? ${UseFileProvider}"$)
Dim TorrentFolder As String=File.Combine(File.DirInternal, TORRENT_FILE_FOLDERNAME)
File.MakeDir("", TorrentFolder)
File.WriteString(TorrentFolder, "myfile.torrent", "This is a text file...")
Dim Intent1 As Intent
Intent1.Initialize(ACTION_DOWNLOAD_TORRENT, "")
Intent1.PutExtra(EXTRA_ABC, "this is an extra string")
SetFileUriAsIntentData(Intent1, TorrentFolder, "myfile.torrent")
Intent1.SetType("application/x-bittorrent")
Dim JavaObject1 As JavaObject
JavaObject1.InitializeContext
JavaObject1.RunMethod("sendBroadcast", Array As Object(Intent1))
End Sub
'Replaces the intent Data field with the file uri.
'Resets the type field. Make sure to call Intent.SetType after calling this method
Sub SetFileUriAsIntentData (Intent As Intent, Dir As String, FileName As String)
Dim jo As JavaObject = Intent
jo.RunMethod("setData", Array(GetFileUri(Dir, FileName)))
Intent.Flags = Bit.Or(Intent.Flags, 1) 'FLAG_GRANT_READ_URI_PERMISSION
End Sub
How can app #2 open the .torrent file in app #1's DirInternal?
Stripping "file://" from the uri string value and trying to access "/data/data/b4a.example.deleteme/files/torrent-files/myfile.torrent" fails with a permission error (as to be expected).