The CommonsBlog


"Elements of Kotlin" Version 0.4 Released

Subscribers now have access to Version 0.4 of Elements of Kotlin, in PDF, EPUB, and MOBI/Kindle formats. Just log into your Warescription page to download it, or set up an account and subscribe!


There are a few new “Kotlin, WTF?” short chapters, on:

And, as usual, there were several bug fixes.

Jan 25, 2021


ACTION_SEND, the Chooser, and ClipData

Roderick Gadellaa pointed out an interesting problem with the “share sheet”. In Android 10+, the share sheet can show a preview of your content. In particular, it can show a preview of an image that you are sharing. The catch is that the share sheet needs to know what image that is… and whether it does depends on how you are sharing it.

Sharing content involves ACTION_SEND, and if we use EXTRA_STREAM to supply a Uri, we need to add FLAG_GRANT_READ_URI_PERMISSION and/or FLAG_GRANT_WRITE_URI_PERMISSION on the Intent:

val intent = Intent(Intent.ACTION_SEND).apply {
    putExtra(Intent.EXTRA_STREAM, uri)
    type = "image/webp"
    addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

Frequently, when using ACTION_SEND, we wrap it in a chooser Intent, such as via Intent.createChooser(). So, we pass our Intent to createChooser() and roll from there:

val intent = Intent(Intent.ACTION_SEND).apply {
  putExtra(Intent.EXTRA_STREAM, uri)
  type = "image/webp"
  addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

startActivity(Intent.createChooser(intent, null))

And, if you try this, you will not get a preview in the share sheet.

This, however, works:

val intent = Intent(Intent.ACTION_SEND).apply {
  clipData = ClipData.newRawUri(null, uri)
  putExtra(Intent.EXTRA_STREAM, uri)
  type = "image/webp"
  addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
}

startActivity(Intent.createChooser(intent, null))

The reason is that the framework copies the ClipData out of the ACTION_SEND and puts it in the ACTION_CHOOSER Intent created by createChooser(). It does not copy any EXTRA_STREAM value, though.

There is also the possibility that you are using ShareCompat.IntentBuilder:

ShareCompat.IntentBuilder.from(this)
  .setType("image/webp")
  .addStream(uri)
  .startChooser()

This too fails… for now. cketti filed an issue for that one, and while it is fixed, I do not believe that fix has shipped yet.

UPDATE 2021-01-13: The fix is available in androidx.core:core:1.5.0-beta01!

Ideally, you use setClipData() on an ACTION_SEND Intent or that repaired version of ShareCompat.IntentBuilder, so your shared images are able to be previewed properly.

Jan 07, 2021


"Elements of Android Jetpack" Version 2.0 Released

Subscribers now have access to an update to Elements of Android Jetpack, known as Version 2.0, in PDF, EPUB, and MOBI/Kindle formats, in addition to the online reader. Just log into your Warescription page and download away, or set up an account and subscribe!


As with all of the CommonsWare x.0 releases, this is the same as the previous release (1.9), other than some bug fixes.

The next significant release will be Version 2.1, which will come out sometime after Android Studio 4.2 ships in stable form. Since that is in an early beta now, my guess is that it will be late January to early February before a stable Android Studio 4.2 ships.

Jan 03, 2021


"Exploring Android" Version 2.0 Released

Subscribers now have access to an update to Exploring Android, known as Version 2.0, in PDF, EPUB, and MOBI/Kindle formats, in addition to the online reader. Just log into your Warescription page and download away, or set up an account and subscribe!


As with all of the CommonsWare x.0 releases, this is the same as the previous release (1.9), other than some bug fixes.

The next significant release will be Version 2.1, which will come out sometime after Android Studio 4.2 ships in stable form.

Dec 14, 2020


"Elements of Android Jetpack" Version 1.9 Released

Subscribers now have access to Version 1.9 of Elements of Android Jetpack, in PDF, EPUB, and MOBI/Kindle formats. Just log into your Warescription page to download it, or set up an account and subscribe!


This is a maintenance update, with minor tweaks for Android Studio 4.1.1. This includes some notes about using the inline emulator (where the emulator appears in a tool window in the IDE itself) and the Database Inspector for examining SQLite databases.

It also replaces all uses of Kotlin synthetic accessors with view binding, since Kotlin synthetic accessors are now deprecated. There also were some legacy findViewById() uses in the Java samples, so those too were updated to use view binding.

A few minor adjustments were made for Android 11, with respect to storage and permissions. This includes switching (for a while) to use android:requestLegacyExternalStorage="true" in the Files chapter’s main sample.

In addition:

  • Many dependencies were updated throughout the app

  • A bunch of bugs were fixed

The next update, to 2.0, should be out in a month and will only have bug fixes. Version 2.1 should come out after Android Studio 4.2 ships a stable release.

Dec 06, 2020


Older Posts