Intent Filters & Receiving

yttrium

Active Member
Licensed User
Longtime User
I've looked up how to receive content from an app when it wants to share something. This usually involves "Intent Filters". For my app, I want it to be able to accept pictures, and then launch the app and save the picture to memory when it's launched (via the share).

How do I go about doing this? Explanations I've seen involve adding something resembling the following to my Manifest file:

B4X:
AddApplicationText(
<intent-filter>
    <action android:name="android.intent.action.SEND" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>)

My app doesn't show up in the stock Gallery's share function with this. How can I make it work, and how can I reference what to do when the app is launched this way? There's nice documentation on this for Android, but not for Basic4Android.
 

yttrium

Active Member
Licensed User
Longtime User
As I said before you need to post what is returned by:
B4X:
Log(In.ExtrasToString)
You cannot pass that directly to GetPathFromContentResult.

Sorry, I figured that since the content chooser worked that the intent would as well. I'll get a printout for you in a little bit.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
The printout of Msgbox(Log(In.ExtrasToString)) is:
B4X:
Bundle[{android.intent.extra.STREAM=content://media/external/images/media/7859}]

Obtained by using the share function on a screenshot in the AOSP4.1 Gallery app.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Okay, so now you get the value of Uri using:
B4X:
Dim UriString as String
UriString = In.GetExtra("android.intent.extra.STREAM")
' Now UriString should contain a value similar to content://media/external/images/media/7859
'And now you can call the function to get the filepath
Preview.SetBackgroundImage(LoadBitmap("", GetPathFromContentResult(UriString)))
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
I'm still getting these exceptions.

B4X:
Error occurred:
An error has occured in sub:
main_getpathfromcontentresult
(java line: 415)
java.lang.RuntimeException:
Object should first be initialized (Cursor).
Continue?

Only difference is from line 408 to 415.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
For some reason I cannot understand this code always returns null.
B4X:
Dim UriString as String
UriString = In.GetExtra("android.intent.extra.STREAM")

I worked around it, not very elegant, but it would work.
B4X:
      Log(In.ExtrasToString)
      Dim MyUriString As String 
      MyUriString = In.ExtrasToString
      MyUriString = MyUriString.SubString2(MyUriString.IndexOf("=")+1,MyUriString.IndexOf(","))
      Log(MyUriString)
      Activity.SetBackgroundImage (LoadBitmap("", GetPathFromContentResultString(MyUriString )))
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
Try to do:

To figure out what is sent back in return and which extra is which.

For some reason I cannot understand this code always returns null.
B4X:
Dim UriString as String
UriString = In.GetExtra("android.intent.extra.STREAM")

I worked around it, not very elegant, but it would work.
B4X:
      Log(In.ExtrasToString)
      Dim MyUriString As String 
      MyUriString = In.ExtrasToString
      MyUriString = MyUriString.SubString2(MyUriString.IndexOf("=")+1,MyUriString.IndexOf(","))
      Log(MyUriString)
      Activity.SetBackgroundImage (LoadBitmap("", GetPathFromContentResultString(MyUriString )))

GetPathFromContentResultString isn't valid, do you mean GetPathFromContentResult?
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User

Your new snippet of code gives the following error:
B4X:
Error Occurred
An error has occurred in sub: main_activity_resume (java line: 337)
java.lang.StringIndexOutOfBoundsException: length=80; regionStart=36; regionLength=-37
Continue?
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
Your new snippet of code gives the following error:
B4X:
Error Occurred
An error has occurred in sub: main_activity_resume (java line: 337)
java.lang.StringIndexOutOfBoundsException: length=80; regionStart=36; regionLength=-37
Continue?
It works fine for me. Maybe your intent is delivered differently.
You will have to figure out that yourself.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
It works fine for me. Maybe your intent is delivered differently.
You will have to figure out that yourself.

It was a really simple error with this line:

B4X:
MyUriString = MyUriString.SubString2(MyUriString.IndexOf("=")+1,MyUriString.IndexOf(","))

I don't know where you thought a comma existed in

B4X:
Bundle[{android.intent.extra.STREAM=content://media/external/images/media/7859}]

but changing the line of code to

B4X:
MyUriString = MyUriString.SubString2(MyUriString.IndexOf("=")+1,MyUriString.IndexOf("}"))

fixed it immediately.

Thanks!
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
It was a really simple error with this line:

B4X:
MyUriString = MyUriString.SubString2(MyUriString.IndexOf("=")+1,MyUriString.IndexOf(","))

I don't know where you thought a comma existed in

B4X:
Bundle[{android.intent.extra.STREAM=content://media/external/images/media/7859}]

but changing the line of code to

B4X:
MyUriString = MyUriString.SubString2(MyUriString.IndexOf("=")+1,MyUriString.IndexOf("}"))

fixed it immediately.

Thanks!
The intent returned on my device, had another extra after this one, so it had a comma.
So you have to careful otherwise your code will not run on all devices (my code was only a demonstration not meant to be used in production-grade code).
Either way, the correct way is to use In.GetExtra which is not working for some reason.
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
The intent returned on my device, had another extra after this one, so it had a comma.
So you have to careful otherwise your code will not run on all devices (my code was only a demonstration not meant to be used in production-grade code).
Either way, the correct way is to use In.GetExtra which is not working for some reason.

Can you show me the syntax of your Log(In.ExtrasToString) on your device?
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
Here:
B4X:
Bundle[{android.intent.extra.STREAM=content://media/external/images/media/7935, exit_on_sent=true}]

So then perhaps this?
B4X:
MyUriString = In.ExtrasToString
MyUriString = MyUriString.SubString2(MyUriString.IndexOf("STREAM=")+1,MyUriString.IndexOf("}"))
If MyUriString.Contains(",") Then MyUriString = MyUriString.SubString2(0,MyUriString.IndexOf(","))
 
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
Upvote 0

yttrium

Active Member
Licensed User
Longtime User
Yes, that seems reasonably safe. :)

Turns out it's not. I am incredibly stupid:

B4X:
MyUriString = In.ExtrasToString
MyUriString = MyUriString.SubString2(MyUriString.IndexOf("STREAM=")+1,MyUriString.IndexOf("}"))
If MyUriString.Contains(",") Then MyUriString = MyUriString.SubString2(0,MyUriString.IndexOf(","))

STREAM= is not 1 character.

B4X:
MyUriString = In.ExtrasToString
MyUriString = MyUriString.SubString2(MyUriString.IndexOf("STREAM=")+7,MyUriString.IndexOf("}"))
If MyUriString.Contains(",") Then MyUriString = MyUriString.SubString2(0,MyUriString.IndexOf(","))

Error fixed and works perfectly, though I'm experimenting with Erel's answer as well (which replaces the whole GetPathFromContentResult function).
 
Last edited:
Upvote 0
Top