Problem Coping File

margret

Well-Known Member
Licensed User
Longtime User
I don't know how B4A works but I finished some code I was writing only to end up with an exception error. The error it is listing is on the destination file and it says file not found. It's not there because I am trying to copy it. The log file is below and you can see the External SDcard is writable. You can also see the variables I am using SDir, SFile, DDir, DFile and the values in each. These directories are there and I can set an imageview to an image in each of these paths and the image displays with no problem, even the image I am trying to copy. It's almost like the File.Copy is trying to work backwards because of the FileNotFoundException being connected to the destination. Any ideas whats going on? The first block is the code and the second the log file.

Thanks,

Margret

Code:
B4X:
Log("SDir: " & SDir)
Log("SFile: " & SFile)
Log("DDir: " & DDir)
Log("DFile: " & DFile)
Log("File.ExternalWritable: " & File.ExternalWritable)
File.Copy(SDir, SFile, DDir, DFile)

Log File:
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
SDir: /mnt/media/My Files/Pictures
SFile: davidwsp.jpg
DDir: /mnt/sdcard/My Files/Pictures
DFile: davidwsp.jpg
File.ExternalWritable: true
main_btnsfok_click (B4A line: 36)
File.Copy(SDir, SFile, DDir, DFile)

java.io.FileNotFoundException: /mnt/sdcard/My Files/Pictures/davidwsp.jpg (Permission denied)
   at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
   at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
   at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
   at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:344)
   at anywheresoftware.b4a.objects.streams.File.Copy(File.java:320)
   at ADR.file.main._btnsfok_click(main.java:288)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:104)
   at anywheresoftware.b4a.BA.raiseEvent2(BA.java:92)
   at anywheresoftware.b4a.BA.raiseEvent(BA.java:88)
   at anywheresoftware.b4a.objects.ViewWrapper$1.onClick(ViewWrapper.java:49)
   at android.view.View.performClick(View.java:2408)
   at android.view.View$PerformClick.run(View.java:8818)
   at android.os.Handler.handleCallback(Handler.java:587)
   at android.os.Handler.dispatchMessage(Handler.java:92)
   at android.os.Looper.loop(Looper.java:123)
   at android.app.ActivityThread.main(ActivityThread.java:4627)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:521)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   at dalvik.system.NativeStart.main(Native Method)
java.io.FileNotFoundException: /mnt/sdcard/My Files/Pictures/davidwsp.jpg (Permission denied)
 

margret

Well-Known Member
Licensed User
Longtime User
File Storage Permissions

File.DirRootExternal and File.DirDefaultExternal have a special side effect of adding the STORAGE permission to your manifest file. I guess that you are not using it and instead writing "/sdcard" or something like that.

Hello Erel,

That was it! It works fine with File.DirRootExternal but I now have to re-write a lot of code. I needed to start at "/mnt" so Media or SDCard could be selected.
So File.DirRootExternal does more than the docs say. I logged both to look at the path I used: "/mnt/sdcard" and the File.DirRootExternal: "mnt/sdcard". Both are the same and they are placed into the same Var As String as this Var is called later. I confirmed this by logging Var and checking Var.Length. Because they are both placed in the same variable and are the same, I fail to see why it works different other than there is more taking place than I can see or than the Docs show for File.DirRootExternal.

Again, Thanks for Your Help!!

Margret
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
It may be easier to insert the permission manually.

Try putting this line
B4X:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in the AndroidManifest.xml file, and check the "Do not overwrite manifest file" option on the project menu, and see if that works.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Permission SDCard

It may be easier to insert the permission manually.

Try putting this line
B4X:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
in the AndroidManifest.xml file, and check the "Do not overwrite manifest file" option on the project menu, and see if that works.

Once again my friend you have helped greatly! After looking at your page and seeing what you were showing me and reading Erels post. I knew that the compiler was modifing the manifest based on seeing the command. So I changed the code back to the way it was at first and just added one line of code. I am not using this line of code but the system is. I added: DummyVar = File.DirRootExternal and it all works fine now. You guys saved me a lot of time!! :sign0142::sign0098::sign0060:

Thanks,

Margret
 
Last edited:
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Once again my friend you have helped greatly! After looking at your page and seeing what you were showing me and reading Erels post. I changed the code back to the way it was at first and just added one line of code. I am not using this line of code but the system is. I added this: DummyVar = File.DirRootExternal and it all works fine now. You guys saved me a lot of time!! :sign0142::sign0098::sign0060:

Thanks,

Margret

Your answer is far slicker than mine :sign0188:
 
Upvote 0

cgalvizu

Member
Licensed User
Longtime User
Hi guys: I am having similar problem. I am trying to download a file and save it in the sd card:
DownloadService.Target = File.OpenOutput(DummyVar , fileName, False)

where
newConfigurationID = "2"
DummyVar = File.DirRootExternal& "/" & newConfigurationID

I am getting the error:
java.io.FileNotFoundException: mnt/sdcard/2/epc.mp4

previously I created that directory with:
File.MakeDir(File.DirRootExternal , newConfigurationID)

any suggestions?
 
Upvote 0
Top