Android Question error while opening pdf intent

Makumbi

Well-Known Member
Licensed User
here is the error i get

B4X:
25/05/2020
25/05/2020
0782911364
[]
6
** Activity (main) Pause, UserClosed = false **
** Activity (sharetoemailaddress) Create, isFirst = true **
** Activity (sharetoemailaddress) Resume **
Status: READY
Class: P1
Category: NEW HOMEWORK READY
NEW HOMEWORK READY
Error occurred on line: 231 (Sharetoemailaddress)
android.os.FileUriExposedException: file:///storage/emulated/0/P1WORK.PDF exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
    at android.net.Uri.checkFileUriExposed(Uri.java:2371)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10247)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10201)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667)
    at android.app.Activity.startActivityForResult(Activity.java:4586)
    at android.app.Activity.startActivityForResult(Activity.java:4544)
    at android.app.Activity.startActivity(Activity.java:4905)
    at android.app.Activity.startActivity(Activity.java:4873)
    at anywheresoftware.b4a.keywords.Common.StartActivity(Common.java:859)
    at com.kccug.kabojjajuniorapp.sharetoemailaddress$ResumableSub_DownloadAndSaveFileP1.resume(sharetoemailaddress.java:2357)



B4X:
    DownloadAndSaveFileP1("http://kccug.com/KabojjaApp/workold/P1WORK.PDF")
Sub DownloadAndSaveFileP1 (Link As String)
 
    Dim j As HttpJob
    j.Initialize("", Me)
    j.Download(Link)
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
      
        Dim out As OutputStream = File.OpenOutput(File.DirInternal, "P1WORK.PDF", False)
        File.Copy2(j.GetInputStream, out)
        out.Close
              
        Dim rp As RuntimePermissions
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            File.Copy(File.DirInternal, "P1WORK.PDF", File.DirRootExternal, "Download/P1WORK.PDF")
            Dim FilePath As String = File.Combine(File.DirRootExternal, "Download/P1WORK.PDF")
            
            Dim Phone As Phone
            If Phone.SdkVersion <= 18 Then           ' min - 4.3.1
                Dim i As Intent
                i.Initialize("android.intent.action.MEDIA_SCANNER_SCAN_FILE", "file://" & FilePath)
                Phone.SendBroadcastIntent(i)
            Else
                Dim ctxt As JavaObject
                ctxt.InitializeContext
                Dim MediaScannerConnection As JavaObject
                MediaScannerConnection.InitializeStatic("android.media.MediaScannerConnection")
                Dim interface As Object = MediaScannerConnection.CreateEventFromUI("android.media.MediaScannerConnection.OnScanCompletedListener", "ScanCompleted", _
                   Null)
                MediaScannerConnection.RunMethod("scanFile", Array(ctxt, Array As String(FilePath), Array As String("image/pdf"), interface))
            End If
        End If
      
    End If
    j.Release
    
    Dim i As Intent
    i.Initialize(i.ACTION_VIEW, "file://" & File.Combine(File.DirRootExternal & "/", "P1WORK.PDF"))
    i.SetComponent("android/com.android.internal.app.ResolverActivity")
    i.SetType("application/pdf")
    StartActivity(i)
    
    
'    MsgboxAsync("The downloaded work has been placed in Downloads of Adobe acrobat reader. Please Install adobe acrobat reader incase it is not yet installed on your phone thank you. And Check in Adobe click files. ","Homework Downloaded")
    ProgressDialogHide
 
Top