我在kotlin中创建用户个人资料, 我想给用户两个选项(从头像或捕获图像中选择) 然后在“sqlite”数据库中保存配置文件
什么是最佳解决方案?
如果你帮我编写代码,我会很感激
答案 0 :(得分:2)
我得到了解决方案:
创建数据库类" DatabaseHelper.kt":
class DatabaseHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, 1) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("Create Table $TABLE_NAME(ID INTEGER PRIMARY KEY AUTOINCREMENT,Category_img BLOB)")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME)
onCreate(db)
}
fun insertData(Category_img: ByteArray): Boolean {
val db = writableDatabase
val cv = ContentValues()
cv.put(CAL_NO1, Category_img)
val result = db.insert(TABLE_NAME, null, cv)
return if (result .equals( -1))
false
else
true
}
fun getdata(): ByteArray {
val db = writableDatabase
val res = db.rawQuery("select * from " + TABLE_NAME, null)
if (res.moveToFirst()) {
do {
return res.getBlob(0)
} while (res.moveToNext())
}
return byteArrayOf()
}
companion object {
private val DATABASE_NAME = "user.db"
private val TABLE_NAME = "tbl_user"
private val CAL_NO1 = "Category_img"
}
}
并从您要访问相机的位置进行编码:
class MainActivity : AppCompatActivity() {
var dbSq: DatabaseHelper= DatabaseHelper(this);
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, 101)
}
public override fun onActivityResult(requestcode: Int, resultcode: Int, intent: Intent) {
super.onActivityResult(requestcode, resultcode, intent)
if (resultcode == Activity.RESULT_OK) {
if (requestcode == 101) {
val photo = intent.extras!!.get("data") as Bitmap
val stream = ByteArrayOutputStream()
photo.compress(Bitmap.CompressFormat.PNG, 100, stream)
val byteArray = stream.toByteArray()
Log.d("check",dbSq.insertData(byteArray).toString().plus(" "));
}
}
}
<强>&#34; DatabaseHelper.kt&#34;:强>
class DatabaseHelper(context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, 1) {
override fun onCreate(db: SQLiteDatabase) {
db.execSQL("Create Table $TABLE_NAME(ID INTEGER PRIMARY KEY AUTOINCREMENT,Category_img Text)")
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME)
onCreate(db)
}
fun insertData(Category_img: String): Boolean {
val db = writableDatabase
val cv = ContentValues()
cv.put(CAL_NO1, Category_img)
val result = db.insert(TABLE_NAME, null, cv)
return if (result .equals( -1))
false
else
true
}
companion object {
private val DATABASE_NAME = "user.db"
private val TABLE_NAME = "tbl_user"
private val CAL_NO1 = "Category_img"
}
}
并从您要访问相机的位置进行编码:
class MainActivity : AppCompatActivity() {
var dbSq: DatabaseHelper= DatabaseHelper(this);
internal var output: File? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
val date = Date()
output = File(dir, "CheckImage " + date.toString()+".jpeg")
startActivityForResult(cameraIntent, 101)
}
public override fun onActivityResult(requestcode: Int, resultcode: Int, intent: Intent) {
super.onActivityResult(requestcode, resultcode, intent)
if (resultcode == Activity.RESULT_OK) {
if (requestcode == 101) {
Log.d("check",dbSq.insertData(output!!.absolutePath).toString().plus(" "));
}
}
}
}