main2activity is not saving the image but I don't know why ?
MainActivity :
Main2Activity
I also added <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
MainActivity :
Code:
import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main2.*
import java.io.ByteArrayOutputStream
import java.lang.Exception
import java.util.jar.Manifest
class Main2Activity : AppCompatActivity() {
var selectedImage: Bitmap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
fun select1(view: View){
if(checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),2)
}else{val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,1)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode==2){
if (grantResults.size > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,1)
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode ==1 && resultCode == Activity.RESULT_OK && data !=null){
val image = data.data
try {
selectedImage = MediaStore.Images.Media.getBitmap(this.contentResolver,image)
imageView.setImageBitmap(selectedImage)
}catch (e:Exception){e.printStackTrace()}
}
super.onActivityResult(requestCode, resultCode, data)
}
fun save(view: View){
val artName = editText.text.toString()
val outputStream = ByteArrayOutputStream()
selectedImage?.compress(Bitmap.CompressFormat.PNG,50,outputStream)
val byteArray = outputStream.toByteArray() // the image as a byteArray
try {
val myDataBase = this.openOrCreateDatabase("Arts", Context.MODE_PRIVATE, null)
myDataBase.execSQL("CREATE TABLE IF NOT EXISTS arts (name VARCHAR, age BLOB)")
//myDataBase.execSQL("INSERT INTO musicians (name,age) VALUES ('$artName',50)")
val values = ContentValues()
values.put("name",artName)
values.put("image",byteArray)
myDataBase.insert("arts",null,values)
}catch (e: Exception){e.printStackTrace()}
val intent = Intent(applicationContext,MainActivity::class.java)
startActivity(intent)
}
}
Main2Activity
Code:
import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main2.*
import java.io.ByteArrayOutputStream
import java.lang.Exception
import java.util.jar.Manifest
class Main2Activity : AppCompatActivity() {
var selectedImage: Bitmap? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
}
fun select1(view: View){
if(checkSelfPermission(android.Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
requestPermissions(arrayOf(android.Manifest.permission.READ_EXTERNAL_STORAGE),2)
}else{val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,1)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
if (requestCode==2){
if (grantResults.size > 0 && grantResults[0]==PackageManager.PERMISSION_GRANTED){
val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
startActivityForResult(intent,1)
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if(requestCode ==1 && resultCode == Activity.RESULT_OK && data !=null){
val image = data.data
try {
selectedImage = MediaStore.Images.Media.getBitmap(this.contentResolver,image)
imageView.setImageBitmap(selectedImage)
}catch (e:Exception){e.printStackTrace()}
}
super.onActivityResult(requestCode, resultCode, data)
}
fun save(view: View){
val artName = editText.text.toString()
val outputStream = ByteArrayOutputStream()
selectedImage?.compress(Bitmap.CompressFormat.PNG,50,outputStream)
val byteArray = outputStream.toByteArray() // the image as a byteArray
try {
val myDataBase = this.openOrCreateDatabase("Arts", Context.MODE_PRIVATE, null)
myDataBase.execSQL("CREATE TABLE IF NOT EXISTS arts (name VARCHAR, age BLOB)")
http://myDataBase.execSQL("INSERT INTO musicians (name,age) VALUES ('$artName',50)")
val values = ContentValues()
values.put("name",artName)
values.put("image",byteArray)
myDataBase.insert("arts",null,values)
}catch (e: Exception){e.printStackTrace()}
val intent = Intent(applicationContext,MainActivity::class.java)
startActivity(intent)
}
}
I also added <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>