Android Question file not found in file.copy

Cliff McKibbin

Member
Licensed User
I have been using Erl's method of sending emails with files attached for several years now.

I had earlier converted all but one of my apps to b4xpages and had no trouble converting the FS92 class that sends these emails.

I just started converting the last app to b4xpages and have gotten hung up on converting my FS92 routine. The file.copy step is crashing with the log show below.

B4X:
sending message to waiting queue of uninitialized activity (submitjob)
*** Receiver (httputils2service) Receive (first time) ***
FileName1:tsrel.txt      ' this is a log line I am writing to try and debug-see logic below
Copying:tsrel.txt         ' ditto
pagefs92_sendfile (java line: 262)
java.io.FileNotFoundException: /data/user/0/b4a.CoOpSupportx/files/shared/tsrel.txt (No such file or directory)
    at java.io.FileOutputStream.open0(Native Method)
    at java.io.FileOutputStream.open(FileOutputStream.java:287)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:223)
    at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:449)
    at anywheresoftware.b4a.objects.streams.File.Copy(File.java:342)
    at b4a.CoOpSupportx.pagefs92._sendfile(pagefs92.java:262)
    at b4a.CoOpSupportx.pagefs92._b4xpage_menuclick(pagefs92.java:138)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:221)
    at anywheresoftware.b4a.BA$1.run(BA.java:360)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6958)
    at java.lang.reflect.Method.invoke(Native Method)

The logic in the program in question is as follows. The starter.fs92helpfile variable was used to display the "Help" screen with a txt message in another 'class'. It displays and the user touches the help menu and 'Send to Email' so I know it is in DirAssets correctly. I also displayed the name in the log as shown in the log above. I added the logs to try and debug.

B4X:
' This logic provided by Erl in his "FileProvider" example and uses the library "FileProvider"
    Dim FileName1 As String = Starter.FS92File
    Log ("FileName1:" & FileName1)
    
    If Starter.FS92Switch =2 Then 
            Log ("Copying:" & FileName1) 
            File.Copy(File.DirAssets, FileName1, Starter.Provider.SharedFolder, FileName1)
            Log ("Copy successful")
    End If

The manifest is set up as recommended in Erl's "FileProvider' Example:

B4X:
AddManifestText(<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />
)
AddManifestText (<uses-permission android:name = "android.permission.ACCESS_FINE_LOCATION"/>)
AddManifestText (<uses-permission android:name = "android.permission.ACCESS_COARSE_LOCATION"/>)

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" />

I have the definition of FileProvider in Starter as:

B4X:
Sub Process_Globals
    ' setup File Provider for email
    Public Provider As FileProvider
....
End Sub

Sub Service_Create
    Provider.Initialize
    ....
end sub

I can't tell from the log error whether the problem is in the source file or in the shared folder output file.
I am a little suspicious of the '0' in the file.copy error message. It might imply that it doesn't know the name of the app and therefore can't find the file. I checked and my app is named in 'Main' in the same way it is named in the programs that worked.

I might also mention that the file.copy worked fine in the original b4a program.

I did find I was using a 'filehandler' class in other programs. I brought that in, but it didn't help.

Thanks for any help. Cliff McKibbin
 

Cliff McKibbin

Member
Licensed User
Erel, Thanks for the question, but it may be moot.
I 'uninstalled' the app a few times and moved the specification and initialization of 'provider' to the FS92 class from Starter. I tried it again and it worked properly.

In parallel, I had realized that my database had become 'readonly'. I was starting to look into that and saw one of your replies from 2016. Again, since I had un-installed the app, it rebuilds the database upon startup following installation and when that happened, it was again read/write. I then restored the original database from the original app and it also is fine.

All of this is very bizarre. The conversion of the app in my process (see below) is piece-meal and perhaps I was missing some crucial thing early on that that corrupted the installation process.



Thanks for all you do for the community.
Cliff McKibbin
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…