我正在制作一个供读书者使用的android应用程序,接下来有3个按钮,上一个和书签(单页)。当用户单击下一个/上一个按钮时,图像会更改,问题是如何将当前图像(显示给用户)保存到sqlite数据库?这是我的MainActivity Java文件:
package com.example.book;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.ByteArrayOutputStream;
public class MainActivity extends AppCompatActivity {
byte[] img1;
Bitmap b;
MyDataBase myDataBase;
SQLiteDatabase database;
Cursor c;
private int passed =0;
private int currentImage = 0;
private ImageView viewImage;
int[] images = {R.drawable.f1,R.drawable.f2,R.drawable.f3,R.drawable.f4,R.drawable.f5,
R.drawable.f6,R.drawable.f7,R.drawable.f8,R.drawable.f9,R.drawable.f10};
@SuppressLint("WrongThread")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
myDataBase=new MyDataBase(getApplicationContext(),"imagedata",null,1);
}
catch (Exception e){
e.printStackTrace();
}
viewImage = findViewById(R.id.viewImage);
Button next = findViewById(R.id.next);
Button previous = findViewById(R.id.previous);
Button bookmark = findViewById(R.id.save);
next.setOnClickListener(nextImage);
previous.setOnClickListener(previousImage);
bookmark.setOnClickListener(save);
}
View.OnClickListener nextImage = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(currentImage == (images.length-1)){
viewImage.setImageResource(images[currentImage]);
}
else
{
currentImage++;
currentImage = currentImage % images.length;
//a%b = a -floor(a/b) *b
viewImage.setImageResource(images[currentImage]);
}
}
};
View.OnClickListener previousImage = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(currentImage==0){
viewImage.setImageResource(images[currentImage]);
}
else{
currentImage--; // if current image is 6
currentImage = (currentImage+images.length) %images.length;
//(5+10) % 10 = 15%10 = 5 5th image is loaded
viewImage.setImageResource(images[currentImage]);
}
}
};
View.OnClickListener save = new View.OnClickListener() {
@Override
public void onClick(View v) {
//get resource
//Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.f1);
//what to do here? how to save current image ? to sqlite?
Bitmap bitmap =((BitmapDrawable)viewImage.getDrawable().);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] img=bos.toByteArray();
database=myDataBase.getWritableDatabase();
// content values is used to create empty set of values using the default initial size
ContentValues cv=new ContentValues();
cv.put("image", img);
database.insert("tableimage", null, cv);
}
};
}
在此行做什么?提取当前图像并将其存储在sqlite中?
Bitmap bitmap =((BitmapDrawable)viewImage.getDrawable().); ???
MyDatabase.java文件也是:
package com.example.book;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyDataBase extends SQLiteOpenHelper {
public MyDataBase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table tableimage (image blob , id int PRIMARY KEY);");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
如果我的方向错误,请帮帮我。