Android Question File Copy from internal DCIM-Folder to any SD-Card-Folder

der_c

Member
Licensed User
Hi at all,

i'm trying to copy a file from the internal memory, folder DCIM, to any (whatever) folder, but I always get an error and the app crashes.

Here is my example:
File.Copy (File.DirInternal & "/DCIM","test.jpg",File.DirRootExternal,"test.jpg")
(Using Android 7.1.2)

What's wrong, I'm grateful for any help.

Stefan
 

der_c

Member
Licensed User
...yes, for sure...says file not found, but why?

Error occurred on line: 100 (Main)
java.io.FileNotFoundException: /data/user/0/b4a.example/files/DCIM/test.jpg (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at anywheresoftware.b4a.objects.streams.File.OpenInput(File.java:214)
at anywheresoftware.b4a.objects.streams.File.Copy(File.java:340)
at b4a.example.main._datumrechnen(main.java:455)
at b4a.example.main._btn30_click(main.java:468)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:180)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:80)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22433)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
 
Last edited:
Upvote 0

josejad

Expert
Licensed User
Longtime User
Have you checked with a file explorer if the file /data/user/0/b4a.example/files/DCIM/test.jpg exits?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
yes, for sure...says file not found, but why?
Since you are trying to copy to: File.DirRootExternal, you need Runtime Permissions to be able to write to the external.Of course you must have a file in File.DirInternal folder. Here is a link that has a ton of information about it. The first item in the link is the video on runtime permisssions by Erel. This topic has been dealt with hundred of times, so there is a l;ot of code to help you get started.
https://www.b4x.com/android/forum/pages/results/?query=runtime+permissions
 
Last edited:
Upvote 0

der_c

Member
Licensed User
ok, thanks, the video is very helpful, but i get an error, don't no why. Here is the code, the manifest and the error.

code....
shared = rp.GetSafeDirDefaultExternal("")
Dim TestFolder As String = rp.GetSafeDirDefaultExternal("Test")
File.WriteString(TestFolder, "test.txt", "1234aaa")
rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
wait for Activity_PermissionResult (permission As String, result As Boolean)
If result Then
Dim Pid As PhoneId
Log(Pid.GetDeviceId)
Else
Log ("No permission")
End If

manifset.....
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
AddManifestText(<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />)

error...
B4A Version: 8.80
Parse den Code. (0.00s)
Building folders structure. (0.03s)
Kompiliere den Code. (0.08s)
Kompiliere Layoutcode. (0.01s)
Organisiere Libraries. (0.00s)
Generiere R Datei. Error
AndroidManifest.xml:21: error: Error parsing XML: not well-formed (invalid token)

Any Idea??
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Any Idea??
Your complete code should be like this:
B4X:
Sub Globals
    Private rp As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim TestFolder As String = rp.GetSafeDirDefaultExternal("Test")
    File.WriteString(TestFolder, "test.txt", "1234aaa")   
    rp.CheckAndRequest(rp.PERMISSION_READ_PHONE_STATE)
    wait for Activity_PermissionResult (permission As String, result As Boolean)
    If result Then
        Dim Pid As PhoneId
        Log(Pid.GetDeviceId)
    Else
        Log ("No permission")
    End If
End Sub

Remove this from manifest:
B4X:
AddManifestText(<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />)
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I ran it and it worked for me. Here attached is the complete project. All you need to do is unzip it and deploy to your phone
 

Attachments

  • DercExample.zip
    7.9 KB · Views: 252
Upvote 0

der_c

Member
Licensed User
sorry no, i got an error...

B4A Version: 8.80
Parse den Code. (0.00s)
Building folders structure. (0.03s)
Kompiliere den Code. (0.03s)
Kompiliere Layoutcode. (0.00s)
Organisiere Libraries. (0.00s)
Generiere R Datei. Error
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex


I'm totally desperate, so easy problem so much trouble.

Stefan
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…