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

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

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"
}

Wrong name of the shared file when choosing the app from Intent.createChooser

My app can share internally stored files with the androidx.core.content.FileProvider.
The intent and the chooser are created with the following snippet:
val shareIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(
Intent.EXTRA_STREAM,
FileProvider.getUriForFile(this, authority, file, displayName),
)
type = document.mimeType.mediaType
}
startActivity(Intent.createChooser(shareIntent, getString(R.string.share_title)))
I explicitly pass the displayName to the FileProvider, but in the app chooser, I see the file's local name (see image).
Is there a way to show the correct display name in the chooser?

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.

Launching Sharesheet without Intent.createChooser

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.

how to open files using intent action with latest Android storage framework?

Since Android introduced major changes in storage framework recently much of the documentation talks about permissions and scoped storage. But I couldn't find details on how to process Uri of a file, for it to be readable by other apps.
The intent action to view/read a file by other apps fail. I don't understand what's the problem here;
Does it have to do with difference between java.io.File and java.nio.File?
The Uri has missing permissions or the Uri is not well formatted.
The Android storage samples (FileManager) has this bug as well. It lists all the files in a directory successfully but can't open a selected image, or a document. I've reported this issue but no help so far.
Following snippet is from FileManager (storage-samples)
fun openFile(activity: AppCompatActivity, selectedItem: File) {
// Get URI and MIME type of file
val uri = Uri.fromFile(selectedItem).normalizeScheme()
val mime: String = getMimeType(uri.toString())
// Open file with user selected app
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.data = uri
intent.type = mime
return activity.startActivity(intent)
}
After the hints from the comments, I found the answer in developer docs.
Caution: If you want to set both the URI and MIME type, don't call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type.
The reason behind openFile didn't throw FileUriExposedException in android-storage-samples is that after setting intent.type, the Uri gets nullified and when I changed it to setDataAndType() I got the exception. The final snippet looks like
fun openFile(activity: AppCompatActivity, selectedItem: File) {
// Get URI and MIME type of file
val uri = FileProvider.getUriForFile(activity.applicationContext, AUTHORITY, selectedItem)
// val uri = Uri.fromFile(selectedItem).normalizeScheme()
val mime: String = getMimeType(uri.toString())
// Open file with user selected app
val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
// intent.data = uri
// intent.type = mime
intent.setDataAndType(uri, mime)
return activity.startActivity(intent)
}
I think they forgot to update the samples over time, let me create a pull request to commit this change over there as well.

Categories

Resources