Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Capture view and Register to gallery

658634c5077d2652d14b6ba043d850f0?s=47 Umechan
January 17, 2020

Capture view and Register to gallery

658634c5077d2652d14b6ba043d850f0?s=128

Umechan

January 17, 2020
Tweet

Transcript

  1. Capture view and Register to gallery

  2. About me Hikaru Umetsu (@umechanhika) WealthNavi Inc. Individual developer Fav:

    Android & Flutter Individual Job
  3. What is “capture”? App screen Image file

  4. Overview Capture view File storage access Register to gallery Summary

  5. Capture view view.isDrawingCacheEnabled = true val cache = view.drawingCache val

    bitmap = Bitmap.createBitmap(cache) val bitmap = view.drawToBitmap() val canvas = Canvas(bitmap) view.draw(canvas) Deprecated New https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt
  6. File storage access In this case: MediaStore API Is this

    ... Media file? Yes Shareable? Yes Removed on app uninstalled? No https://developer.android.com/training/data-storage
  7. Permission https://developer.android.com/training/data-storage No need on Android 10 or higher <uses-permission

    android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28" />
  8. Register to gallery Android 10 or higher Android 9 or

    lower Create ContentValues Save bitmap Create ContentValues Save bitmap Register to MediaStore Register to MediaStore
  9. Register to gallery Android 10 or higher Android 9 or

    lower Create ContentValues Save bitmap Create ContentValues Save bitmap Register to MediaStore Register to MediaStore
  10. Register to gallery ContentValues().apply { put(MediaStore.Images.Media.DISPLAY_NAME, "fileName.jpg") put(MediaStore.Images.Media.TITLE, "fileName") put(MediaStore.Images.Media.MIME_TYPE,

    "image/jpeg") put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1_000) } Create ContentValues https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt Common
  11. Register to gallery ContentValues().apply { put(MediaStore.Images.Media.DISPLAY_NAME, "fileName.jpg") put(MediaStore.Images.Media.TITLE, "fileName") put(MediaStore.Images.Media.MIME_TYPE,

    "image/jpeg") put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1_000) } Create ContentValues https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt Common
  12. Register to gallery ContentValues().apply { put(MediaStore.Images.Media.DISPLAY_NAME, "fileName.jpg") put(MediaStore.Images.Media.TITLE, "fileName") put(MediaStore.Images.Media.MIME_TYPE,

    "image/jpeg") put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1_000) } Create ContentValues https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt Common
  13. Register to gallery ContentValues().apply { put(MediaStore.Images.Media.DISPLAY_NAME, "fileName.jpg") put(MediaStore.Images.Media.TITLE, "fileName") put(MediaStore.Images.Media.MIME_TYPE,

    "image/jpeg") put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1_000) } Create ContentValues https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt Common
  14. Register to gallery ContentValues().apply { put(MediaStore.Images.Media.DISPLAY_NAME, "fileName.jpg") put(MediaStore.Images.Media.TITLE, "fileName") put(MediaStore.Images.Media.MIME_TYPE,

    "image/jpeg") put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1_000) } Create ContentValues https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt Common
  15. Register to gallery Register to MediaStore contentValues.apply { put( MediaStore.Images.Media.RELATIVE_PATH,

    "${Environment.DIRECTORY_PICTURES}/DirectoryName" ) put(MediaStore.MediaColumns.IS_PENDING, 1) } contentResolver.run { uri = insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues) ?: return // ... } Android 10 or higher https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt
  16. Register to gallery Save bitmap contentResolver.run { uri = insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

    contentValues) ?: return openOutputStream(uri).use { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, it) } contentValues.put(MediaStore.MediaColumns.IS_PENDING, 0) update(uri, contentValues, null, null) } Android 10 or higher https://github.com/umechanhika/playground/blob/master/android/AddScreenshotToGallery/app/ src/main/java/com/example/addscreenshottogallery/ui/util/ScreenshotUtil.kt
  17. Completed

  18. Drawing cache is deprecated. Use MediaStore API. No need permission

    on Android 10 or higher Register process is different from Android 10 or higher Summary
  19. Thanks!