Android Question How can use try catch to trap error in case file is not there

Makumbi

Well-Known Member
Licensed User
B4X:
Dim FileName As String = "APPNOTES.PDF"
        'copy the shared file to the shared folder
        File.Copy(File.DirInternal, FileName  , Starter.Provider.SharedFolder, FileName )
        Dim email As Email
        email.To.Add(EmailAddress.Text)
        email.Subject = "Homework"
        email.Attachments.Add(Starter.Provider.GetFileUri(FileName ))
        'email.Attachments.Add(Starter.Provider.GetFileUri(FileName )) 'second attachment
        Dim in As Intent = email.GetIntent
        in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
        StartActivity(in)
        ProgressDialogHide
    End If
 

MicroDrie

Well-Known Member
Licensed User
Longtime User
Makumbi what about preventing a error? Why not do a simple test:

B4X:
If File.Exists(Starter.Provider.GetFileUri(FileName1)) = True Then ' first attachment
    ' Add your first file here
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName1))
End If
If File.Exists(Starter.Provider.GetFileUri(FileName2)) = True Then ' second attachment
    ' Add your second file here
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName2))
End If

Be aware that you need read rights which you can check with runtimepermissins rp.CheckAndRequest before the first if statement. If you want you can place the code between the Try Catch code

B4X:
    Try
        
    Catch
        Log(LastException)
    End Try
 
Upvote 0

Makumbi

Well-Known Member
Licensed User
Makumbi what about preventing a error? Why not do a simple test:

B4X:
If File.Exists(Starter.Provider.GetFileUri(FileName1)) = True Then ' first attachment
    ' Add your first file here
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName1))
End If
If File.Exists(Starter.Provider.GetFileUri(FileName2)) = True Then ' second attachment
    ' Add your second file here
    email.Attachments.Add(Starter.Provider.GetFileUri(FileName2))
End If

Be aware that you need read rights which you can check with runtimepermissins rp.CheckAndRequest before the first if statement. If you want you can place the code between the Try Catch code

B4X:
    Try
       
    Catch
        Log(LastException)
    End Try

Will this work out i was trying it this please advise

B4X:
    Try
            Dim FileName As String = "CURCULARS.PDF"
            'copy the shared file to the shared folder
        
            File.Copy(File.DirInternal, FileName  , Starter.Provider.SharedFolder, FileName )
            Dim email As Email
            email.To.Add(EmailAddress.Text)
            email.Subject = "Homework"
            '    email.Attachments.Add(Starter.Provider.GetFileUri(FileName ))
            email.Attachments.Add(Starter.Provider.GetFileUri(FileName )) 'second attachment
            Dim in As Intent = email.GetIntent
            in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
            StartActivity(in)
            ProgressDialogHide
        Catch
            MsgboxAsync("The File Not Detected Please Try Again. ","Homework Downloaded")
        End Try
 
Upvote 0
Top