fix #47, handle Image Capture intent

This commit is contained in:
tibbi 2017-10-22 12:22:32 +02:00
parent e805baf6eb
commit 6c04557a82
3 changed files with 48 additions and 13 deletions

View File

@ -46,6 +46,11 @@
<category android:name="android.intent.category.DEFAULT"/> <category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/> <data android:mimeType="image/*"/>
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.media.action.IMAGE_CAPTURE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity> </activity>
<activity <activity

View File

@ -1,12 +1,15 @@
package com.simplemobiletools.draw.activities package com.simplemobiletools.draw.activities
import android.app.Activity
import android.content.Intent import android.content.Intent
import android.graphics.Bitmap import android.graphics.Bitmap
import android.graphics.PorterDuff import android.graphics.PorterDuff
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore
import android.support.v4.content.FileProvider import android.support.v4.content.FileProvider
import android.util.Log
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.webkit.MimeTypeMap import android.webkit.MimeTypeMap
@ -40,6 +43,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
private var strokeWidth = 0f private var strokeWidth = 0f
private var suggestedFileExtension = PNG private var suggestedFileExtension = PNG
private var isEraserOn = false private var isEraserOn = false
private var isImageCaptureIntent = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -79,11 +83,18 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
override fun onCreateOptionsMenu(menu: Menu): Boolean { override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu, menu) menuInflater.inflate(R.menu.menu, menu)
menu.apply {
findItem(R.id.menu_confirm).isVisible = isImageCaptureIntent
findItem(R.id.menu_save).isVisible = !isImageCaptureIntent
findItem(R.id.menu_share).isVisible = !isImageCaptureIntent
}
return true return true
} }
override fun onOptionsItemSelected(item: MenuItem): Boolean { override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_confirm -> confirmImage()
R.id.menu_save -> trySaveImage() R.id.menu_save -> trySaveImage()
R.id.menu_share -> shareImage() R.id.menu_share -> shareImage()
R.id.clear -> clearCanvas() R.id.clear -> clearCanvas()
@ -105,17 +116,14 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
} }
private fun tryOpenFile() { private fun tryOpenFile() {
handlePermission(PERMISSION_WRITE_STORAGE) { getStoragePermission {
if (it) { openFile()
openFile()
} else {
toast(R.string.no_storage_permissions)
}
} }
} }
private fun openFile() { private fun openFile() {
FilePickerDialog(this, curPath) { val path = if (isImageCaptureIntent) "" else curPath
FilePickerDialog(this, path) {
openPath(it) openPath(it)
} }
} }
@ -141,6 +149,16 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
openPath(path) openPath(path)
} }
} }
if (intent?.action == MediaStore.ACTION_IMAGE_CAPTURE) {
val output = intent.extras?.get(MediaStore.EXTRA_OUTPUT)
if (output != null && output is Uri) {
isImageCaptureIntent = true
Log.e("DEBUG", "output $output, path ${output.path}")
curPath = output.path
invalidateOptionsMenu()
}
}
} }
private fun getStoragePermission(callback: () -> Unit) { private fun getStoragePermission(callback: () -> Unit) {
@ -217,13 +235,20 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
} }
} }
private fun trySaveImage() { private fun confirmImage() {
handlePermission(PERMISSION_WRITE_STORAGE) { val file = File(curPath)
if (it) { getFileOutputStream(file) {
saveImage() it.use {
} else { my_canvas.getBitmap().compress(file.getCompressionFormat(), 70, it)
toast(R.string.no_storage_permissions)
} }
setResult(Activity.RESULT_OK)
finish()
}
}
private fun trySaveImage() {
getStoragePermission {
saveImage()
} }
} }

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_confirm"
android:icon="@drawable/ic_check"
android:title="@string/save"
app:showAsAction="ifRoom"/>
<item <item
android:id="@+id/menu_save" android:id="@+id/menu_save"
android:icon="@drawable/ic_save" android:icon="@drawable/ic_save"