我目前在存储和检索数据库中的图像时遇到问题。全部设置但程序停止,同时单击按钮保存并从数据库中获取图片。该错误在我的Log cat中显示为“无法执行活动方法”。
请帮帮我!以下是代码。
DATABASEHELPER.java
Fabric.with(this, new Crashlytics.Builder().core(
new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build());
MainActivity.java
package com.example.image_database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.widget.ImageView;
class DatabaseHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME = "test.db";
private static final int DATABASE_VERSION = 1;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("Create table if not exists tb(a blob)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
activity_main.xml中
package com.example.image_database;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageView image1;
SQLiteDatabase db;
Button btn1,btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image1 = (ImageView) this.findViewById(R.id.image1);
btn1 = (Button) this.findViewById(R.id.save);
btn2 = (Button) this.findViewById(R.id.get);
}
public void saveImage(View v)
{
try {
FileInputStream fis = new FileInputStream("/storage/sdcard/4.jpg");
try {
byte[] image = new byte[fis.available()];
fis.read(image);
ContentValues values = new ContentValues();
values.put("a", image);
db.insert("tb", null, values);
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void getImage(View v)
{
Cursor c = db.rawQuery("Select * from tb", null);
if(c.moveToNext())
{
byte[] image = c.getBlob(0);
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,image.length);
image1.setImageBitmap(bmp);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
在Android sqlite db上存储数据库中的图像并不酷?您应该将图像存储在文件夹中。