Launching Sharesheet without Intent.createChooser - android

Is there a way to launch the Sharesheet (see https://developer.android.com/training/sharing/send) without having to wrap the called intent into the createChooser-returned intent, but rather specifying the wish for the Sharesheet as an ACTION and/or EXTRA. E.g. instead of
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
startActivity(shareIntent)
Something like:
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_CREATE_CHOOSER
putExtra(Intent.EXTRA_ACTION_TYPE, Intent.ACTION_SEND)
putExtra(Intent.EXTRA_TEXT, "This is my text to send.")
type = "text/plain"
}
startActivity(shareIntent)
My main goal is to get a Sharesheet that shows my frequent contacts/apps at the top. I'm not interested in all the functionality that createChooser might provide.
Should work at least in Android 11.

Related

Is it possible to send image with default sms in android?

I am trying to send image with default android message app. There are three conditions.
I don't want to show intent chooser, which means I don't want to show share bottom sheet.
I want to send message with image.
Have to use Intent.ACTION_SENDTO, not Intent.ACTION_SEND. When I use Intent.ACTION_SEND, it shows intent chooser. I have to send message directly to the default android app.
I tried several code, but it's hard to send message with image. Here are some code that I tried.
1.
val intent = Intent(Intent.ACTION_SENDTO).apply {
putExtra("sms_body", "hello")
putExtra(Intent.EXTRA_STREAM, uri)
data = Uri.parse("smsto:12345")
}
val intent = Intent(Intent.ACTION_SENDTO).apply {
putExtra("sms_body", "hello")
putExtra(Intent.EXTRA_STREAM, uri)
data = Uri.parse("smsto:12345")
type = "image/png"
}

Add image to share intent for a specific app

I'm trying to implement share logic in my application. Intent contains just a basic text, but there are applications that I need to pass additional image. For that reason I am building intent like this:
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "image/jpeg"
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
putExtra(Intent.EXTRA_TEXT, intentText)
//putExtra(Intent.EXTRA_STREAM, fileToShare)
}
val share = Intent.createChooser(intent, null)
share.putExtra(
Intent.EXTRA_REPLACEMENT_EXTRAS, bundleOf(
"com.google.android.gm" to bundleOf(
Intent.EXTRA_STREAM to fileToShare
),
"com.whatsapp" to bundleOf(
Intent.EXTRA_STREAM to fileToShare
),
)
)
The problem is the apps that need to receive image cant load passed to fileToShare image. There are no errors, just a Toast in Telegram that says that something like Bad file format. In the same time if i just pass exactly same image not in createChooser but in regular intent construction (take a look at the commented line) all works just fine.

Send thumbnail with text - no app can perform this action - error

I would like to display a small preview image of my content to be shared. However, when I run the code, I get a message that no app can perform this action. Unfortunately I don't know what's wrong
private fun shareContentWithPreview() {
val share = Intent.createChooser(Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "https://developer.android.com/training/sharing/")
type = "image/jpg"
// (Optional) Here we're setting the title of the content
putExtra(Intent.EXTRA_TITLE, "Introducing content previews")
// (Optional) Here we're passing a content URI to an image to be displayed
val uri: Uri = Uri.parse("android.resource://com.one.myapplication/drawable/rome.jpg")
data = uri
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}, null)
startActivity(Intent.createChooser(share, null))
}
Hi m8 think you are passing putExtra(Intent.EXTRA_TEXT, ...), and you should pass putExtra(Intent.EXTRA_STREAM, ...) if you a re passing a Uri

Android string resource links outside of app

I want to have a share intent where it sends the message "here" and when the user clicks on the message, it takes them to my app in the google play store. Currently I'm not getting any text at all in the message
val shareMessage = SpannableString("Here")
shareMessage.setSpan(URLSpan("https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID), 0, 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, shareMessage)
type = "text/plain"
}
val shareIntent = Intent.createChooser(sendIntent, null)
if (shareIntent.resolveActivity(context.packageManager) != null) {
context.startActivity(shareIntent)
}

No apps can perform this action Android Intent using Kotlin

I am using the following code snippets from Android official documentation to share content through applications using Intent but it says "No apps can perform this action." on a physical device. I have messengers, email client and text message clients installed.
val intent = Intent().apply {
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, "Text to share")
}
startActivity(Intent.createChooser(intent, "Sharing"))
I think you should change your intent initialization with apply to this:
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Text to share")
}
When you modify the intent variable inside the apply you are modifying the activity intent not the brand new intent.
copy this code and you will see what i'm talking about:
val intent_1 = Intent().apply {
intent.action = Intent.ACTION_SEND
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, "Text to share")
}
This is what I know:
As Fredy Mederos said, the value that you are modified is the Activity.getIntent, not the new Intent.
You should write like this:
val intent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "Text to share")
}
or more precise:
val intent = Intent().apply {
this.action = Intent.ACTION_SEND
this.type = "text/plain"
this.putExtra(Intent.EXTRA_TEXT, "Text to share")
}
the this is pointed to your initialized new Intent().
The following code works instead of above posted in the question.
val i = Intent(Intent.ACTION_SEND)
i.type = "text/plain"
i.putExtra(Intent.EXTRA_TEXT, "Content to share")
startActivity(Intent.createChooser(i, "Sharing"))
I am not sure why the code in the question does not work but my guess is that intent is related to the activity's intent and it works when I instantiated another object from Intent class.
you can replace startActivity(Intent.createChooser(i, "Sharing"))
by startActivity(i)

Categories

Resources