Android Question Intent Error?

Dogbonesix

Active Member
Licensed User
Longtime User
Every thing was working fine until one day I clicked on a imageview expecting the intent to show applications to launch. But it seems I get this error - Expenses.png exposed beyond app through Intent.getData(). Why do I get this error - what does it mean? And how can I to fix it. Any help will be appreciated.
 

Peter Simpson

Expert
Licensed User
Longtime User
@Dogbonesix that sounds like a file provider issue which started from target SDK 24+ (Android N) I do believe, that's when it all changed. Read this tutorial from Erel to learn more about sharing files to other apps.

CLICK HERE.

Enjoy...
 
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
Thanks for the reply. Still having problems - I looked at the tutorial and created app using the sample but still having error - I have attached my application - hopefully it can help me understand the problem. Thanks for any help.
 

Attachments

  • FileShareProblen.zip
    10.1 KB · Views: 149
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
1. You are using TargetSDK 26.
android.jar / targetSdkVersion / minSdkVersion

You are not using FileProvider here. WHY?
B4X:
        Intent1.Initialize(Intent1.ACTION_VIEW, "file://" & File.DirRootExternal & "/" & Label2.text)
        Intent1.SetComponent("android/com.android.internal.app.ResolverActivity")

FileProvider

You are NOT using Runtimepermissions in your App.
RuntimePermissions

I suggest to read the Info in th first link i provided. Read it as much until you did understand it.
 
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
I have added the Runtimepermissions and attached the changes.

You ask why I am not using FileProvider in the intent line - I am not sure how to do that.

Also there is a problem with the lines below:

If p.SdkVersion >= 24 Then
uri = CreateFileProviderUri(Starter.shared, FileToSend)
Else
uri = "file://" & File.Combine(Starter.shared, FileToSend)
End If
Whatever the reason it says unknown member: shared.

Thanks for the support
 

Attachments

  • FileShareProblen.zip
    10.1 KB · Views: 153
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
I've downloaded the sample above several times and run it hundreds of times - literally. I have studied the code extensively. The very first time I ran it several apps were provided to launch the file(1.bal). Every time since the the message "Not Supported" appears. This error occurs now no matter how many times I have downloaded it, ran it or removed it - but it did run properly once.
 
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
I have the sample working but it really does not solve the problem. The attached file runs fine if the file is in the dir.assets folder but if the file is in a local folder (.../Documents/red.png) I receive an permissions error. And if I need to copy a file to a shared location how do I save any changes because any change would not reflect to the original file? Any insight deeply appreciated.
 

Attachments

  • IntentFile.zip
    67.1 KB · Views: 147
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
a local folder (.../Documents/red.png)
There is no such folder in Android.

I receive an permissions error
Please post the error.

And if I need to copy a file to a shared location how do I save any changes because any change would not reflect to the original file?
Copy it again before you share it.

Note that there is a new FileProvider class that will make things simpler: https://www.b4x.com/android/forum/threads/97865/#content
 
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
The sample attached is a snippet of my full application and in my application I create a folder called "documents". When I click on a file in the "documents" folder I expect it to launch - BUT - I get this error "java.io.FileNotFoundException: /storage/emulated/0/documents/red.png(Permission denied).

When you run the attached file there are two buttons. Button One uses the path File.DirAssets that runs just fine. Button two uses the same code but the file path uses a local folder that is when I get error. Also, I used to be able to copy the text in the Log but now I can't.

I will check out the next FileProvider class. There has to be a simpler way to click on a file and have it launch...
 

Attachments

  • IntentFile.zip
    67.3 KB · Views: 139
Upvote 0

Dogbonesix

Active Member
Licensed User
Longtime User
The FileProvider class example does have exactly what I need and it almost works. I duplicated the ViewButton to ViewButton2. In button2 I changed the path from DirAssets to a local path. The local path contains a folder (that I created) called "documents". In the "documents" folder I have a file called "red.png". Button2 uses the local path and catches and displays any errors. ViewButton2, that is using the local path, always gets an error "Access Denied". See attach app.
 

Attachments

  • FileProviderError.zip
    20.9 KB · Views: 151
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Why aren't you posting the error message?
B4X:
File.Copy(TestPath, FileName, Starter.Provider.SharedFolder, FileName)

The value of TestPath is:
B4X:
TestPath = "TestPath = " & File.DirRootExternal & "/documents"

This is of course not a valid path.

You should also go over the runtime permissions tutorial and learn how to access File.DirRootExternal.
As it has nothing to do with FileProvider, please start a new thread if you want to further discuss it.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Hello @Dogbonesix,
The following code will work for you, it's been fully tested and is working 100%.

  1. You don't need "TestPath = ", or I should say that you are not using it correctly further down in your code.
  2. You didn't add any RuntimePermissions
  3. Make sure "/documents" is "/documents" and not "/Documents"
  4. Make sure "red.png" is "red.png" and not "Red.png"
  5. For points 3 and 4, if need be you can always use '.ToLowerCase' thus you will not come across any case issues.
Anyway here is your modified code fully working. I just added RuntimePermissions and also and created a folder and file that matched yours and it work perfect.

B4X:
Sub Process_Globals
End Sub

Sub Globals
    Private TestPath As String
    Private Label1 As Label
    Private btn2ViewImage As Button
    Private RP As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    RP.CheckAndRequest(RP.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
  
    'Don't miss the code in the manifest editor!!!
    Activity.LoadLayout("1")
    TestPath = File.DirRootExternal & "/documents"
    Label1.Text = "TestPath = " & TestPath
End Sub

Sub btnShareFile_Click
    Dim FileToSend As String = "Message.txt"
    File.WriteString(Starter.Provider.SharedFolder, FileToSend, "jaklsdjalksdjalskdjasld")
    Dim in As Intent
        in.Initialize(in.ACTION_SEND, "")
        in.SetType("text/plain")
        in.PutExtra("android.intent.extra.STREAM", Starter.Provider.GetFileUri(FileToSend))
        in.Flags = 1 'FLAG_GRANT_READ_URI_PERMISSION
    StartActivity(in)
End Sub

Sub btnViewImage_Click
    Dim FileName As String = "b4a.png"
    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
    Dim in As Intent
        in.Initialize(in.ACTION_VIEW, "")
    Starter.Provider.SetFileUriAsIntentData(in, FileName)
    'Type must be set after calling SetFileUriAsIntentData
    in.SetType("image/*")
    StartActivity(in)
End Sub

Sub btnSendEmail_Click
    Dim FileName As String = "b4a.png"
    'copy the shared file to the shared folder
    File.Copy(File.DirAssets, FileName, Starter.Provider.SharedFolder, FileName)
    Dim email As Email
        email.To.Add("aaa@bbb.com")
        email.Subject = "subject"
        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)
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
End Sub

Sub btn2ViewImage_Click
    Try
        Dim FileName As String = "red.png"
        File.Copy(TestPath, FileName, Starter.Provider.SharedFolder, FileName)
        Dim in As Intent
        in.Initialize(in.ACTION_VIEW, "")
        Starter.Provider.SetFileUriAsIntentData(in, FileName)
        'Type must be set after calling SetFileUriAsIntentData
        in.SetType("image/*")
        StartActivity(in)
    Catch
        Msgbox(LastException.Message,"")
    End Try
End Sub

Enjoy...
 
Last edited:
Upvote 0

Similar Threads

Top