Android Question samsung save jpg file in rootdefaultexternal.... impossibile?

fifiddu70

Well-Known Member
Licensed User
hello, i need to save file in rootdefault external, in 80% smartphones worked, in smartphone samsung i have error, i report one line of error: android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/xxxx.xxxxx.xxxxx/files/ticket.jpg exposed beyond app through ClipData.Item.getUri()
because worked in non samsung smartphone and not worked in samsung smartphones?
 

DonManfred

Expert
Licensed User

fifiddu70

Well-Known Member
Licensed User
DonManfred you have a little code of example? i need to save withouth error in sasmung phones, my project app use rootexternal for save jpeg file.
i post my part of code for save file.
B4X:
Sub boarding
    btndata.Visible=True
    If switch = 0 Then
        i.Initialize("")
        imgload.Bitmap = LoadBitmap(File.DirAssets,"ticket.jpg")
        b.Initialize(File.DirAssets,  "ticket.jpg")
        brect.Initialize(0,0,imgload.Width, imgload.Height)
        c.Initialize(imgload)
        c.DrawColor(Colors.White)
        c.DrawBitmap(b, Null, brect)
    
        c.DrawText(txtdate.Text, 12%x,imgload.Height-51%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtnumberfly.Text, 14%x,imgload.Height-36%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtclockgates.Text, 15%x,imgload.Height-20%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black, "LEFT")
        c.DrawText(txtseatnumber.Text, 15%x,imgload.Height-5%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtfrom.Text, 32%x,imgload.Height-50%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtto.Text, 32%x,imgload.Height-35%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black, "LEFT")
        c.DrawText(txtclok.Text, 43%x,imgload.Height-21%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtpassenger.Text, 32%x,imgload.Height-5%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black, "LEFT")
        i.Invalidate
    
        'shared = rp.GetSafeDirDefaultExternal("")
        rp.CheckAndRequest ( rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult ( Permission As String, Result As Boolean)
        If Result Then         
            
            Dim out As OutputStream
            'Dim testfolder As String
            'testfolder = rp.GetSafeDirDefaultExternal("ticket")
            rp.GetSafeDirDefaultExternal(File.DirDefaultExternal)
            out = File.OpenOutput(File.DirDefaultExternal, "ticket.jpg", False)
            c.Bitmap.WriteToStream(out, 100, "JPEG")
            out.Close
            b = ResizeBitmap(LoadBitmap(File.DirDefaultExternal, "ticket.jpg"), 800, 500)
            
            Else  
        
        End If   
        txtdate.Text=""
        txtnumberfly.Text=""
        txtclockgates.Text=""
        txtseatnumber.Text=""
        txtfrom.Text=""
        txtto.Text=""
        txtclok.Text=""
        txtpassenger.Text=""
    

        
        share.sharebinary ("file://" & File.DirDefaultExternal &  "/" & "ticket.jpg", "image/jpg", "CONDIVIDI xxxx con:", "Ti Piace? Condividila con i tuoi amici")
        
    
    
        
    else if switch = 1 Then
        i.Initialize("")
        imgload.Bitmap = LoadBitmap(File.DirAssets,"ticket2.jpg")
        b.Initialize(File.DirAssets,  "ticket2.jpg")
        brect.Initialize(0,0,imgload.Width, imgload.Height)
        c.Initialize(imgload)
        c.DrawColor(Colors.White)
        c.DrawBitmap(b, Null, brect)
    
        c.DrawText(txtdate.Text, 12%x,imgload.Height-51%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtnumberfly.Text, 14%x,imgload.Height-36%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtclockgates.Text, 15%x,imgload.Height-20%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black, "LEFT")
        c.DrawText(txtseatnumber.Text, 15%x,imgload.Height-5%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtfrom.Text, 32%x,imgload.Height-50%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtto.Text, 32%x,imgload.Height-35%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black, "LEFT")
        c.DrawText(txtclok.Text, 43%x,imgload.Height-21%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black , "LEFT")
        c.DrawText(txtpassenger.Text, 32%x,imgload.Height-5%y,tf.CreateNew(Typeface.SERIF, Typeface.STYLE_BOLD), 20, Colors.Black, "LEFT")
        i.Invalidate
    
    
        rp.CheckAndRequest ( rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult ( Permission As String, Result As Boolean)
        If Result Then
            Dim out As OutputStream
            rp.GetSafeDirDefaultExternal(File.DirDefaultExternal)
            out = File.OpenOutput(File.DirDefaultExternal, "ticket2.jpg", False)
            c.Bitmap.WriteToStream(out, 100, "JPEG")
            out.Close
            b = ResizeBitmap(LoadBitmap(File.DirDefaultExternal, "ticket2.jpg"), 800, 500)
            
        Else
                
        
        End If
    
        txtdate.Text=""
        txtnumberfly.Text=""
        txtclockgates.Text=""
        txtseatnumber.Text=""
        txtfrom.Text=""
        txtto.Text=""
        txtclok.Text=""
        txtpassenger.Text=""
    

    
        share.sharebinary ("file://" & File.DirDefaultExternal &  "/" & "ticket2.jpg", "image/jpg", "CONDIVIDI APP CON:", "Ti Piace Quest'App? Condividila con i tuoi amici")
        
    End If
End Sub
with this code i make a jpeg file with personal insert text with canvas and share in facebook, when save file in rootexternal, the samsung phone go in error and crash.
 

DonManfred

Expert
Licensed User
DonManfred you have a little code of example?
Check the tutorial i linked above. There are some FileProvider Examples linked. Also you can use the forumsearch to find similar questions and the answers.
Do not expect ME to do the research for you. I KNOW that they are there as i build my apps on them.
the samsung phone go in error and crash
READ THE Tutorial i linked above!!!! Based on your code and your answer i fear you DID NOT.

Again:
24 - must use FileProvider when sharing files (https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/)

https://www.b4x.com/android/forum/threads/class-fileprovider-share-files.97865/

After investigating several issues with the current FileProvider code that you can find in the forum, I decided to make some improvements and implement it in a class.

Starting from Android 7 (API 24) you cannot directly share file uris with other applications. You need to use FileProvider.

The FileProvider class should work on all Android versions (4+).

FILE Uris are NO LONGER allowed

To store a File on the external storage you need to use Runtimepermissons.
But to share it you need to use Fileprovider
 
Last edited:
Top