diff options
author | Marvin Borner | 2018-08-31 22:44:44 +0200 |
---|---|---|
committer | Marvin Borner | 2018-08-31 22:44:44 +0200 |
commit | 770081bb0090e8867b1b6e153f8cf65cb700be86 (patch) | |
tree | 8d13eddd3c1498f090ac086d86359a725d273328 /app/src/main/java/me/texx/Texx/CameraActivity.kt | |
parent | 0387275f1111975a3643d4c44c36d105f476fe6e (diff) |
Finished saving and displaying of image
Diffstat (limited to 'app/src/main/java/me/texx/Texx/CameraActivity.kt')
-rw-r--r-- | app/src/main/java/me/texx/Texx/CameraActivity.kt | 35 |
1 files changed, 29 insertions, 6 deletions
diff --git a/app/src/main/java/me/texx/Texx/CameraActivity.kt b/app/src/main/java/me/texx/Texx/CameraActivity.kt index 0023dcc..1cc4800 100644 --- a/app/src/main/java/me/texx/Texx/CameraActivity.kt +++ b/app/src/main/java/me/texx/Texx/CameraActivity.kt @@ -20,7 +20,11 @@ import daio.io.dresscode.matchDressCode import kotlinx.android.synthetic.main.activity_camera.* import me.texx.Texx.util.ThemeUtil.getThemeName import org.jetbrains.anko.intentFor +import org.jetbrains.anko.longToast import java.io.File +import java.io.FileNotFoundException +import java.io.FileOutputStream +import java.io.IOException import java.text.SimpleDateFormat import java.util.* @@ -54,8 +58,9 @@ class CameraActivity : AppCompatActivity() { private fun setListeners() { camera.addCameraListener(object : CameraListener() { override fun onPictureTaken(jpeg: ByteArray?) { - val file: File? = saveImage(BitmapFactory.decodeByteArray(jpeg, 0, jpeg!!.size)) - startActivity(intentFor<MediaPreviewActivity>("file" to file)) + val file: File? = createFile() + writeFile(BitmapFactory.decodeByteArray(jpeg, 0, jpeg!!.size), file) + startActivity(intentFor<MediaPreviewActivity>("filepath" to file.toString())) } }) @@ -80,18 +85,36 @@ class CameraActivity : AppCompatActivity() { /** * Saves [bitmap] in local storage and returns the absolute path [String] of the file */ - private fun saveImage(bitmap: Bitmap): File? { + private fun writeFile(bitmap: Bitmap, file: File?) { + if (file == null) { + longToast("Error creating file - please check the storage permission.)") + return + } + try { + val out = FileOutputStream(file) + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out) + out.close() + } catch (e: FileNotFoundException) { + longToast("File not found - please try again.\n(${e.message})") + } catch (e: IOException) { + longToast("Error accessing file - please try again.\n(${e.message})") + } + } + + /** + * Creates empty file to write the file on + */ + private fun createFile(): File? { val mediaStorageDir = File(Environment.getExternalStorageDirectory().toString() - + "/Android/data/" + + "/Android/media/" + applicationContext.packageName - + "/Files") + + "/Images") if (!mediaStorageDir.exists()) { if (!mediaStorageDir.mkdirs()) { return null } } - val mediaFile: File mediaFile = File(mediaStorageDir.path + File.separator + generateFilename()) return mediaFile |