Problem Opening image in gallery.

grant1842

Active Member
Licensed User
Longtime User
Hi all i am trying to open an image with the default gallery on a phone.

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
    camera1.StartPreview
   
    Dim out As OutputStream
    out = File.OpenOutput(File.DirRootExternal, "Image.jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
   out.Close
    
   ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "Image.jpg"), True)
   Dim i As Intent 
    i.Initialize(i.ACTION_VIEW, File.Combine(File.DirRootExternal, "Image.jpg"))
    i.SetType("image/*")
    StartActivity(i)

    
End Sub

on the phone I getting error.

The Application Gallery has stopped unexpectedly .
Does anyone know what might be the fault here.
 

NJDude

Expert
Licensed User
Longtime User
Try this (Add a reference to the StringUtils lib):
B4X:
Dim su As StringUtils

FilePath = File.Combine(File.DirRootExternal, "Image.jpg"))

Dim i As Intent
i.Initialize(i.ACTION_VIEW, "file://" & su.EncodeUrl(FilePath, "UTF8"))
i.SetType("image/*")
StartActivity(i)
 
Upvote 0

grant1842

Active Member
Licensed User
Longtime User
Thanks NjDude.
THis works better .
I get a toast message file not found.
Is there some documentation on this action_view and the use of the file:// ?

B4X:
 Sub Camera1_PictureTaken (Data() As Byte)
    camera1.StartPreview
   
    Dim out As OutputStream
    out = File.OpenOutput(File.DirRootExternal, "Image.jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
   out.Close
    
'   ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "Image.jpg"), True)
   Dim su As StringUtils
   Dim Filepath
   Filepath = File.Combine(File.DirRootExternal, "Image.jpg")
   Dim i As Intent
   i.Initialize(i.ACTION_VIEW, "file://" & su.EncodeUrl(Filepath, "UTF8"))
   i.SetType("image/*")
   StartActivity(i)
    
End Sub
 
Last edited:
Upvote 0

grant1842

Active Member
Licensed User
Longtime User
I did cast it as string and same thing.
I did a log and got this.


PackageAdded: package:com.grant.onetouch
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
error:(Intent) Intent { act=android.intent.action.VIEW dat=file://%2Fmnt%2Fsdcard%2FImage.jpg typ=image/* flg=0x20000 }
** Activity (main) Pause, UserClosed = false **

B4X:
Sub Camera1_PictureTaken (Data() As Byte)
    camera1.StartPreview
   
    Dim out As OutputStream
    out = File.OpenOutput(File.DirRootExternal&"/DCIM/Camera", "Image.jpg", False)
    out.WriteBytes(Data, 0, Data.Length)
   out.Close
    
   ToastMessageShow("Image saved: " & File.Combine(File.DirRootExternal, "Image.jpg"), True)
   Dim su As StringUtils
   Dim Filepath As String
   Filepath = File.Combine(File.DirRootExternal, "Image.jpg")
   Dim i As Intent
   i.Initialize(i.ACTION_VIEW, "file://" & su.EncodeUrl(Filepath, "UTF8"))
   i.SetType("image/*")
   StartActivity(i)
    Log("error:"&I)
End Sub
 
Upvote 0

NJDude

Expert
Licensed User
Longtime User
Oops!!, actually you only have to URL encode the file name, that's in case that a picture has spaces in the name, in this case you could do this:

B4X:
i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirRootExternal, "Image.jpg"))

I'm not at my developing PC so I couldn't test the code, my bad :D
 
Upvote 0

grant1842

Active Member
Licensed User
Longtime User
Ok so the problem was the url encoding

This is working.

Thanks for your help.
 
Last edited:
Upvote 0
Top