我想从手机画廊中挑选一张图像并将其存储在SqLite数据库中。我有将代码存储到数据库名称,电子邮件,地址等的代码,但是我不知道如何从手机库中选择图像并将该图像存储到SqLite数据库中。如果您能帮助我,我将非常感激,其余的将图像放入ListView中,我想我知道该怎么做。 这是我的DatabaseHelper的代码:
package com.example.zadacaeden;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "test.db";
public static final String TABLE_NAME = "kompanii_table";
public static final String COL_1 = "id";
public static final String COL_2 = "name";
public static final String COL_3 = "address";
public static final String COL_4 = "phone";
public static final String COL_5 = "website";
public static final String COL_6 = "email";
public static final String COL_7 = "latitude";
public static final String COL_8 = "longitude";
public static final String COL_9 = "kategorija";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
//SQLiteDatabase db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLE_NAME+"( id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, address VARCHAR, phone VARCHAR," +
" website VARCHAR, email VARCHAR, latitude VARCHAR, longitude VARCHAR, kategorija VARCHAR )" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
// ZA DA SE VNESE DATATA
public boolean insertData(String name, String address, String phone, String website, String email, String latitude, String longitude, String kategorija){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, name);
contentValues.put(COL_3, address);
contentValues.put(COL_4, phone);
contentValues.put(COL_5, website);
contentValues.put(COL_6, email);
contentValues.put(COL_7, latitude);
contentValues.put(COL_8, longitude);
contentValues.put(COL_9, kategorija);
long result = db.insert(TABLE_NAME, null, contentValues);
if(result == -1){
return false;
}else {
return true;
}
}
// ZA DA SE PRINTA DATATA OD BAZATA U LISTI - SERVICES
public ArrayList<Item> getDataServices(){
ArrayList<Item> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
//SELECT columnName FROM yourTable WHERE CONTAINS (columnName, ‘yourSubstring’);
String query = "select * from "+TABLE_NAME +" where "+ COL_9 +" like '%Servis%'";
Cursor cursor = db.rawQuery(query, null);
while (cursor.moveToNext()){
String ime = cursor.getString(1);
String adresa = cursor.getString(2);
String telefon = cursor.getString(3);
String websajt = cursor.getString(4);
Item item = new Item(ime, adresa, telefon, websajt);
arrayList.add(item);
}
return arrayList;
}
// ZA DA SE PRINTA DATATA OD BAZATA U LISTI - FUN
public ArrayList<Item> getDataFun(){
ArrayList<Item> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
//SELECT columnName FROM yourTable WHERE CONTAINS (columnName, ‘yourSubstring’);
String query = "select * from "+TABLE_NAME +" where "+ COL_9 +" like '%Zabava%'";
Cursor cursor = db.rawQuery(query, null);
while (cursor.moveToNext()){
String ime = cursor.getString(1);
String adresa = cursor.getString(2);
String telefon = cursor.getString(3);
String websajt = cursor.getString(4);
Item item = new Item(ime, adresa, telefon, websajt);
arrayList.add(item);
}
return arrayList;
}
// ZA DA SE PRINTA DATATA OD BAZATA U LISTI - INDUSTRY
public ArrayList<Item> getDataIndustry(){
ArrayList<Item> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
//SELECT columnName FROM yourTable WHERE CONTAINS (columnName, ‘yourSubstring’);
String query = "select * from "+TABLE_NAME +" where "+ COL_9 +" like '%Industrija%'";
Cursor cursor = db.rawQuery(query, null);
while (cursor.moveToNext()){
String ime = cursor.getString(1);
String adresa = cursor.getString(2);
String telefon = cursor.getString(3);
String websajt = cursor.getString(4);
Item item = new Item(ime, adresa, telefon, websajt);
arrayList.add(item);
}
return arrayList;
}
// ZA DA SE PRINTA DATATA OD BAZATA U LISTI - EDUCATION
public ArrayList<Item> getDataEducation(){
ArrayList<Item> arrayList = new ArrayList<>();
SQLiteDatabase db = this.getReadableDatabase();
//SELECT columnName FROM yourTable WHERE CONTAINS (columnName, ‘yourSubstring’);
String query = "select * from "+TABLE_NAME +" where "+ COL_9 +" like '%Edukacija%'";
Cursor cursor = db.rawQuery(query, null);
while (cursor.moveToNext()){
String ime = cursor.getString(1);
String adresa = cursor.getString(2);
String telefon = cursor.getString(3);
String websajt = cursor.getString(4);
Item item = new Item(ime, adresa, telefon, websajt);
arrayList.add(item);
}
return arrayList;
}}
这是我的活动类,我在其中输入存储在数据库中的所有内容的值:
package com.example.zadacaeden;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
public class Vnesuvanje extends AppCompatActivity {
// ZA CHECKBOX
ArrayList<String> selection = new ArrayList<String>();
public String final_checkB_selection = "";
// ZA BAZA
CheckBox servis;
CheckBox zabava;
CheckBox industrija;
CheckBox edukacija;
///////////
ListView ll;
DatabaseHelper databaseHelper;
ArrayList<Item> arrayList;
MyAdapter myAdapter;
Button buttonSave;
EditText editIme, editAdresa, editTelefon, editWebsajt, editEmail, editLatitude, editLongitude;
// Za image
Button btnChooseImage;
private static final int GALLERY_REQUEST_CODE = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vnesuvanje);
//ActionBar menu
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.drawable.logo);
// CheckBoxs
servis = (CheckBox) findViewById(R.id.Services);
zabava = (CheckBox) findViewById(R.id.Fun);
industrija = (CheckBox) findViewById(R.id.Industry);
edukacija = (CheckBox) findViewById(R.id.Education);
// Edit Texts
editIme = (EditText) findViewById(R.id.Ime);
editAdresa = (EditText) findViewById(R.id.Adresa);
editTelefon = (EditText) findViewById(R.id.Telefon);
editWebsajt = (EditText) findViewById(R.id.WebSite);
editEmail = (EditText) findViewById(R.id.Email);
editLatitude = (EditText) findViewById(R.id.Latitude);
editLongitude = (EditText) findViewById(R.id.Longitude);
// Button
buttonSave = (Button) findViewById(R.id.btnSave);
// ListView / DataBase / ArrayList
ll = (ListView) findViewById(R.id.listView);
databaseHelper = new DatabaseHelper(this);
arrayList = new ArrayList<>();
AddData();
}
// ZA DA SE DODADE DATATA VO TABELA
public void AddData(){
buttonSave.setOnClickListener(
new View.OnClickListener(){
@Override
public void onClick(View v){
boolean isInserted = databaseHelper.insertData(
editIme.getText().toString(),
editAdresa.getText().toString(),
editTelefon.getText().toString(),
editWebsajt.getText().toString(),
editEmail.getText().toString(),
editLatitude.getText().toString(),
editLongitude.getText().toString(),
final_checkB_selection
);
if(isInserted == true) {
Toast.makeText(Vnesuvanje.this, "Data Inserted", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(Vnesuvanje.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}
}
);
}
// CHECKBOX TEST
public void selectItem(View view) {
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.Services:
if(checked)
{
selection.add("Servis");
System.out.println(selection);
}
else if(!checked)
{
selection.remove("Servis");
System.out.println(selection);
}
break;
case R.id.Fun:
if(checked)
{
selection.add("Zabava");
System.out.println(selection);
}
else if(!checked)
{
selection.remove("Zabava");
System.out.println(selection);
}
break;
case R.id.Industry:
if(checked)
{
selection.add("Industrija");
System.out.println(selection);
}
else if(!checked)
{
selection.remove("Industrija");
System.out.println(selection);
}
break;
case R.id.Education:
if(checked)
{
selection.add("Edukacija");
System.out.println(selection);
}
else if(!checked)
{
selection.remove("Edukacija");
System.out.println(selection);
}
break;
}
final_checkB_selection = selection.toString();
}}
在此活动中,我将listData中的数据放入数据库:
package com.example.zadacaeden;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import static android.view.View.OnClickListener;
public class ServicesScreen extends AppCompatActivity {
// implements SearchView.OnQueryTextListener
// x i y za swipe/slide - left/right
float x1, x2, y1, y2;
//za click na slikite imagebutton
private ImageButton button;
//Za print od baza u ListView
ListView ll;
DatabaseHelper databaseHelper;
public static ArrayList<Item> arrayList;
MyAdapter myAdapter;
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_services_screen);
//ActionBar menu
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setIcon(R.drawable.logo);
//Image button services on click
button = findViewById(R.id.ServicesImageBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
openServicesScreen();
}
});
//Image button fun on click
button = findViewById(R.id.FunImageBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
openFunScreen();
}
});
//Image button industry on click
button = findViewById(R.id.IndustryImageBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
openIndustryScreen();
}
});
//Image button education on click
button = findViewById(R.id.EducationImageBtn);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
openEducationScreen();
}
});
//ListView
ll = (ListView) findViewById(R.id.listView);
databaseHelper = new DatabaseHelper(this);
arrayList = new ArrayList<>();
loadDataInListView();
// SEARCH
search();
//
/////////////////////////START SWIPE LEFT-RIGHT ON LISTVIEW/////////////
SwipeListViewTouchListener touchListener =
new SwipeListViewTouchListener(
ll,
new SwipeListViewTouchListener.OnSwipeCallback() {
@Override
public void onSwipeLeft(ListView listView, int[] reverseSortedPositions) {
Intent i = new Intent(ServicesScreen.this, FunScreen.class);
startActivity(i);
}
@Override
public void onSwipeRight(ListView listView, int[] reverseSortedPositions) {
Intent i = new Intent(ServicesScreen.this, MainActivity.class);
startActivity(i);
}
},
true, // example : left action = dismiss
false); // example : right action without dismiss animation
ll.setOnTouchListener(touchListener);
/////////////////////////////////END SWIPE LEFT-RIGHT ON LISTVIEW///////////////////////////////////////////////
}
//SEARCH VO LISTI////////////////
public void search(){
EditText theFilter = (EditText) findViewById(R.id.searchFilter);
theFilter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
(ServicesScreen.this).myAdapter.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) { }
});
}
// END SEARCH VO LISTI////////////////
/////////ZA VNESUVANJE SCREEN KLIK/////////////////////////////
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.example_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.item1) {
Intent intent = new Intent(this, Vnesuvanje.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
////////// END ZA VNESUVANJE SCREEN KLIK ////////////////////////////
// DATABAZA da gi printa vo listView
public void loadDataInListView(){
arrayList = databaseHelper.getDataServices();
myAdapter = new MyAdapter(this, arrayList);
ll.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
//ImageButton Open ServicesScreen function
public void openServicesScreen() {
Intent intent = new Intent(this, ServicesScreen.class);
startActivity(intent);
}
//ImageButton Open FunScreen function
public void openFunScreen() {
Intent intent = new Intent(this, FunScreen.class);
startActivity(intent);
}
//ImageButton Open IndustryScreen function
public void openIndustryScreen() {
Intent intent = new Intent(this, IndustryScreen.class);
startActivity(intent);
}
//ImageButton Open EducationScreen function
public void openEducationScreen() {
Intent intent = new Intent(this, EducationScreen.class);
startActivity(intent);
}
//Swipe left - right
public boolean onTouchEvent(MotionEvent touchEvent){
switch (touchEvent.getAction()){
case MotionEvent.ACTION_DOWN:
x1 = touchEvent.getX();
y1 = touchEvent.getY();
break;
case MotionEvent.ACTION_UP:
x2 = touchEvent.getX();
y2 = touchEvent.getY();
if(x1 > x2){
Intent i = new Intent(ServicesScreen.this, FunScreen.class);
startActivity(i);
} else if(x1 < x2){
Intent i = new Intent(ServicesScreen.this, MainActivity.class);
startActivity(i);
}
break;
}
return false;
}
//END Swipe left - right
}
答案 0 :(得分:0)
将SQLite中的BLOB用于您的目的。
此外,您还必须为类型为byte []的数据类添加新字段(在Kotlin中为ByteArray)
使用此代码将可绘制对象转换为byte []:
Drawable d;
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
当您要从数据库检索数据时,此代码可将byte []转换为可绘制对象:
BitmapDrawable(BitmapFactory.decodeByteArray(blob, 0, blob.size))
blob-DB的字节[]
希望这会有所帮助