Android Question SDK 29 : how copy file from another (my) app

BugNot

Member
Hi all,

As you know, only a few months before the obligatory switch to targetSdkVersion 29.

I have 2 applications with database, one free and one full.

Until now, in the free, my database is in File.DirDefaultExternal, and when the user installs the complete one, I copied the database from the free to the complete one.
So until SDK28, with the RuntimePermissions.PERMISSION_WRITE_EXTERNAL_STORAGE permission I could easily recover the database from free to full.

Now with SDK 29 I can no longer do this ☹.

How can I get this file back? There must be some place left on the phone's "hard drive" to do this?

Thank you.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

BugNot

Member
Hi Erel,

I will therefore look the solution based at the intents and now with this, i can use File.DirInternal and share my file.

thanks
 
Last edited:
Upvote 0

BugNot

Member
I progress...

In my app FREE:
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,
   <external-files-path name="name" path="shared" />
)

B4X:
sub transfert
File.Copy(File.DirInternal, "myDataBase.db", Starter.shared, "myDataBase.db")

        Dim in As Intent
        in.Initialize(in.ACTION_SEND, CreateFileProviderUri(Starter.shared, "myDataBase.db"))
        in.SetType("*/*")
        in.Flags = 1
        StartActivity(in)
End Sub

Sub CreateFileProviderUri (Dir As String, FileName As String) As Object
    Dim FileProvider As JavaObject
    Dim context As JavaObject
    context.InitializeContext
    FileProvider.InitializeStatic("android.support.v4.content.FileProvider")
    Dim f As JavaObject
    f.InitializeNewInstance("java.io.File", Array(Dir, FileName))
    Return FileProvider.RunMethod("getUriForFile", Array(context, Application.PackageName & ".provider", f))
End Sub

In my FULL app:
B4X:
AddApplicationText(
  AddActivityText(Main,
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" />
</intent-filter>)

  <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="shared" />
)

B4X:
Sub Activity_Resume

sql1.Close
                           
    If IsRelevantIntent(Activity.GetStartingIntent) Then
        Dim starting As Intent = Activity.GetStartingIntent
        'Log(starting)
        'Log(starting.Action)
        If starting.GetData.StartsWith("content") Then
            Dim starting As Intent = Activity.GetStartingIntent
            Dim uri As String = starting.GetData
            Try
                Log("Loading from: " & uri)
                File.Delete(File.DirInternal,"myDataBase.db")
                File.Copy("ContentDir", uri, File.DirInternal, "myDataBase.db")
            Catch
                Log(LastException)
            End Try
        Else
            Dim in As JavaObject = Activity.GetStartingIntent
            Dim uri As String = in.RunMethod("getParcelableExtra", Array("android.intent.extra.STREAM"))
            Try
                Log("Loading from: " & uri)
                File.Delete(File.DirInternal,"myDataBase.db")
                File.Copy("ContentDir", uri, File.DirInternal, "myDataBase.db")
            Catch
                Log(LastException)
            End Try
        End If
    End If
                   
    sql1.Initialize(File.DirInternal,"myDataBase.db",False)
End Sub

This system work.

Is it possible to transfer multiple files (myDataBase.db, two .txt files and many images)?

When I share, I have a list of applications appearing, is it possible to display only my full app?

Thanks
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
When I share, I have a list of applications appearing, is it possible to display only my full app?
You can use Intent.SetPackage.

Is it possible to transfer multiple files (myDataBase.db, two .txt files and many images)?
It will be simpler to zip everything that you need and return it as a single file.
 
Upvote 0
Top