main2activity is not saving the image but I don’t know why ?
the listView on MainActivity show NOTHING
MainActivity :
MainActivity :
Code:
package com.example.picgallery
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.activity_main.*
import java.lang.Exception
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val artNameArray = ArrayList<String>()
val artImageArray = ArrayList<Bitmap>()
val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, artNameArray)
listView.adapter = arrayAdapter
try {
val dataBase = this.openOrCreateDatabase("Arts", Context.MODE_PRIVATE,null)
dataBase.execSQL("CREATE TABLE IF NOT EXISTS arts (name VARCHAR, image BLOB)")
val cursor = dataBase.rawQuery("SELECT * FROM arts", null)
val nameIx = cursor.getColumnIndex("name")
val imageIx = cursor.getColumnIndex("image")
cursor.moveToFirst()
while (cursor != null){
artNameArray.add(cursor.getString(nameIx))
val byteArray = cursor.getBlob(imageIx)
val image = BitmapFactory.decodeByteArray(byteArray,0,byteArray.size)
artImageArray.add(image)
cursor.moveToNext()
arrayAdapter.notifyDataSetChanged()
}
cursor?.close()
}catch (e:Exception){e.printStackTrace()}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
val menuInflater = menuInflater
menuInflater.inflate(R.menu.add_art,menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem?): Boolean {
if (item?.itemId == R.id.add_art){
val intent = Intent(applicationContext,Main2Activity::class.java)
startActivity(intent)
}
return super.onOptionsItemSelected(item)
}
}
Main2Activity :
Code:
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>