Android Question Saving a picture

Stefano Di Chiano

Active Member
Licensed User
Hi,
I'm using the following code to save a picture that I previously loaded from gallery:
B4X:
        Dim out As OutputStream
        If File.Exists(File.DirRootExternal, "Uppeee/profile.jpg") Then
            out = File.OpenOutput(File.DirRootExternal, "Uppeee/profile.jpg", False)
            bmp.WriteToStream(out, 100, "JPEG")
        Else
            File.MakeDir(File.DirRootExternal, "Uppeee")
            out = File.OpenOutput(File.DirRootExternal, "Uppeee/profile.jpg", False)
            bmp.WriteToStream(out, 100, "JPEG")
        End If
        out.Close
but when I run it, I get the following error:
sending message to waiting queue (OnActivityResult)
running waiting messages (1)
Error occurred on line: 196 (Profilo)
java.io.FileNotFoundException: /data/user/0/it.uppeee.uppeee/files/Uppeee/profile.jpg (No such file or directory)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:448)
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.raiseEvent(BA.java:193)
at anywheresoftware.b4a.phone.Phone$ContentChooser$1.ResultArrived(Phone.java:865)
at anywheresoftware.b4a.BA$4.run(BA.java:585)
at anywheresoftware.b4a.BA.setActivityPaused(BA.java:459)
at it.uppeee.uppeee.profilo$ResumeMessage.run(profilo.java:313)
at android.os.Handler.handleCallback(Handler.java:761)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6517)
at java.lang.reflect.Method.invoke(Native Method)

Even if I have the picture in the gallery I need to save it as a profile picture. So the next time the user log in, I can already show the picture without having the user to select it again.
Line 196 is: "out = File.OpenOutput(File.DirInternal, "Uppeee/profile.jpg", False)". I don't get why I get the "No such file or directory" error even if I'm using the MakeDir function just the line before.

Can anyone help me solve this issue?
 

ronell

Well-Known Member
Licensed User
Longtime User
use GetSafeDirDefaultExternal

B4X:
Sub Process_Globals
    Private rp As RuntimePermissions
End Sub

    Dim out As OutputStream
        If File.Exists(rp.GetSafeDirDefaultExternal("Uppeee"), "profile.jpg") Then
            out = File.OpenOutput(rp.GetSafeDirDefaultExternal("Uppeee"), "profile.jpg", False)
            bmp.WriteToStream(out, 100, "JPEG")
        Else
          
            out = File.OpenOutput(rp.GetSafeDirDefaultExternal("Uppeee"), "profile.jpg", False)
            bmp.WriteToStream(out, 100, "JPEG")
        End If
    out.Close
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Should I use GetSafeDirDefaultExternal or DirInternal?
DirInternal refers to storage that is inside the app's private area. Judging by this statement ...
So the next time the user log in, I can already show the picture without having the user to select it again.
... that is exactly what you need. Using external storage of any type is more complicated and I cannot see how it would give you any benefits. Use DirInternal.
 
Upvote 0
Top