java.io.FileNotFoundException on fileInputStream

Hi everybody I write this after i read all the topic about to read and write files on Android and AppInventor above all the very interesting and useful Some basics on Android storage system wrote by @Anke but after this I still I have my problem with files evidently I don't understand how to correct use.
I explain what I am trying to do. I am developing an extension to manage image file.
With the app I take a picture,
I retrieve the path where there is the file,
I send the path to the extension, at this point I have the java.io.FileNotFoundException


Here the blocks:
I take the picture and with Canvas.SaveAs I save the image
In a TextBox I write the path
I check if the path is correct with the Image1.Picture (works!)
I call the extension
the exception is here:
appendLog("File Position: "+ OBJECT_LOCATION);
File file = new File(OBJECT_LOCATION);
FileInputStream fileInputStream = new FileInputStream(file);
appendLog is a function to create a Log file to check what happens
here the log file:

File Position: file:///storage/emulated/0/CB.jpg
java.io.FileNotFoundException: file:/storage/emulated/0/CB.jpg: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:496)
at java.io.FileInputStream.(FileInputStream.java:159)
at FileStack.FileStack.sFTS(FileStack.java:108)
at FileStack.FileStack.uploadToFileStack(FileStack.java:73)
at java.lang.reflect.Method.invoke(Native Method)
at gnu.expr.PrimProcedure.apply(PrimProcedure.java:285)
at gnu.mapping.CallContext.runUntilDone(CallContext.java:234)
at gnu.mapping.CallContext.runUntilValue(CallContext.java:298)
at gnu.mapping.MethodProc.applyN(MethodProc.java:113)
at gnu.kawa.reflect.Invoke.applyN(Invoke.java:193)
at gnu.kawa.functions.ApplyToArgs.applyN(ApplyToArgs.java:139)
at gnu.kawa.functions.Apply.applyN(Apply.java:70)
at gnu.mapping.ProcedureN.apply2(ProcedureN.java:39)
at com.google.youngandroid.runtime.callComponentMethod(runtime4183767458063774083.scm:1061)
at java.lang.reflect.Method.invoke(Native Method)
at gnu.expr.PrimProcedure.apply(PrimProcedure.java:285)
at gnu.mapping.CallContext.runUntilDone(CallContext.java:234)
at gnu.mapping.CallContext.runUntilValue(CallContext.java:298)
at gnu.mapping.MethodProc.applyN(MethodProc.java:113)
at gnu.kawa.functions.ApplyToArgs.applyN(ApplyToArgs.java:139)
at gnu.kawa.functions.Apply.applyN(Apply.java:70)
at gnu.mapping.ProcedureN.apply2(ProcedureN.java:39)
at edu.mit.appinventor.aicompanion3.Screen1.dispatchEvent(Screen1.yail:10130)
at com.google.appinventor.components.runtime.EventDispatcher.delegateDispatchEvent(EventDispatcher.java:222)
at com.google.appinventor.components.runtime.EventDispatcher.dispatchEvent(EventDispatcher.java:199)
at com.google.appinventor.components.runtime.Button.Click(Button.java:50)
at com.google.appinventor.components.runtime.Button.click(Button.java:42)
at com.google.appinventor.components.runtime.ButtonBase.onClick(ButtonBase.java:780)
at android.view.View.performClick(View.java:7192)
at android.view.View.performClickInternal(View.java:7166)
at android.view.View.access$3500(View.java:824)
at android.view.View$PerformClick.run(View.java:27592)
at android.os.Handler.handleCallback(Handler.java:888)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:213)
at android.app.ActivityThread.main(ActivityThread.java:8178)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:513)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1101)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Linux.open(Native Method)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)
at libcore.io.ForwardingOs.open(ForwardingOs.java:167)
at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8046)
at libcore.io.IoBridge.open(IoBridge.java:482)
... 38 more

The path is correct, but for unknown reason (for me) the file is not found.
I don't understand why in the exception message there aren't two slash.

Well I hope I wrote everything and I hope You can help me, because I have a great headache.
Thank You
Best Regards
Marco

Show the value of global path

What android version and what filescope are you using?

What does taking a picture have to do with saving this image using Canvas?

grafik

To shorten it, post the aia and explain precisely what the goal is.

Which Android version and what did you set for DefaultFileScope in the Designer?

My Android is 10,
DefaultFileScope is Shared

about : What does taking a picture have to do with saving this image using Canvas ?
why not? I only want to have an image saved on the phone to test the extension then if there is a better way no problem I am here to learn.
This app is only a test for the extension that I am developing, the goal of the extension is to upload an image file on a web server

Thank You
Marco
UploadImage.aia (12.3 KB)

Testing in companion on Android 10, I get no error, but no content in the response textbox

Where is the FileStack, where does the image go ?

Hi @TIMAI2, I did not post the extension because there are some personal data and it is not yet ready (obviously) but You should have a log file called FileStackLog.txt with the exception.
Here the source
FileStack.txt (6.7 KB)

Thank You
Marco

I'll need to leave this. I do not work with file storage other than to the ASD, it seems pointless when there is a workable read/write location for files...

Ok, but as I said: The taken picture (with Camera comp.) has nothing to do with your Canvas image!

Post a link to this extension (or is it your own, not published ext?).

English is not my language so maybe I don't understand what you want to tell me.
In my app, after taken the picture, i set the canvas background image with the image taken (this is the connection between the camera and canvas) the i save the file with the name CB.jpg, tht is an image taken from the camera.

Btw, this path wont work on Android 11+ ...
There you have no access to the root dir of the external storage.

To work with ASD files what steps I have to do?
DefaultFileScope: App or Private ? or something else?

App,
but you told us you've already read my guide.

1 Like

The App just started

Take the picture

AfterPicture set as background of Canvas and the saved

After click on button Image to check if the path is correct

Last the image saved CB.jpg in the root

This is what happen in my Android 10 phone

Well I try with App
thank You for yor patience

Marco

Still not sure why you need to create/save the canvas background image if you already have the camera image?

Well this is the first time that I work with image, so i found this way to save the image using canvas with the name file that I want, if you point me to use the image created with camera well no problem, better, but I suppose that not change the main problem of FileNotFoundExecption, but why not to try
Thank You @TIMAI2
Marco

Ok sorry, I've overlooked something.
Try with ASD or this, it should work on all Android version:

If you can post necessary code from your extension then the topic can be moved to Extension Development - MIT App Inventor Community category where more people will be able to help you.

1 Like

I solved, the problem about java.ioFileNotFoundException was
the line code: File file = new File(OBJECT_LOCATION);
if OBJECT_LOCATION is file:///........ becomes file:/...... so the path is incorrect
How I solved, instead to use:
java.io.File;
I used:
java.nio.file.Path;
java.nio.file.Paths;

I changed ->
File file = new File(OBJECT_LOCATION);
with ->
Path path = Paths.get(URI.create(OBJECT_LOCATION));
and then to read the file
byte[] buffer = java.nio.file.Files.readAllBytes(path);

Easy....after You understand where the problem lies

Ciao
Marco