【发布时间】:2017-10-25 09:09:04
【问题描述】:
我正在尝试将图像插入SQLite 数据库但我得到null 点异常,但检查没有任何null object。我尽力了我的水平,但我仍然找不到帮助我的问题。我尝试过的代码如下所示。谢谢堆栈溢出,请帮助我,
MainActivity.class
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button button;
Bitmap bitmap;
Context context;
LocalDatabase localDatabase;
public static final int PICK_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=(ImageView)findViewById(R.id.imageid);
localDatabase=new LocalDatabase(this);
button=(Button)findViewById(R.id.btnid);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PICK_IMAGE) {
Uri uri=data.getData();
imageView.setImageURI(uri);
InputStream iStream = null;
try {
iStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
byte[] inputData = getBytes(iStream);
saveimage(inputData);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
boolean saveimage(byte[] bytes)
{
localDatabase.insertImage(bytes);
return true;
}
}
LocalDataBase.class
public class LocalDatabase extends SQLiteOpenHelper {
public static final String IMAGE_ID = "id";
public static final String IMAGE = "image";
LocalDatabase localDatabase;
private SQLiteDatabase mDb;
private static final String DATABASE_NAME = "Images.db";
private static final int DATABASE_VERSION = 1;
private static final String IMAGES_TABLE = "ImagesTable";
private static final String CREATE_IMAGES_TABLE =
"CREATE TABLE " + IMAGES_TABLE + " (" +
IMAGE_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ IMAGE + " BLOB NOT NULL );";
public LocalDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
mDb.execSQL(CREATE_IMAGES_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + CREATE_IMAGES_TABLE);
onCreate(sqLiteDatabase);
}
// Insert the image to the Sqlite DB
public void insertImage(byte[] imageBytes) {
mDb=localDatabase.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(IMAGE, imageBytes);
mDb.insert(IMAGES_TABLE, null, cv);
localDatabase.close();
}
// Get the image from SQLite DB
// We will just get the last image we just saved for convenience...
public byte[] retreiveImageFromDB() {
Cursor cur = mDb.query(true, IMAGES_TABLE, new String[]{IMAGE,},
null, null, null, null,
IMAGE_ID + " DESC", "1");
if (cur.moveToFirst()) {
byte[] blob = cur.getBlob(cur.getColumnIndex(IMAGE));
cur.close();
return blob;
}
cur.close();
return null;
}
}
【问题讨论】:
-
将图像插入 SQLite 数据库是不好的做法
-
现在我正在尝试将图像插入到表格中
-
一个好的做法是简单地存储图像路径(或URL)。不是物理数据。
-
除了所有 cmets 向您解释这样做不是一个好主意之外,您还应该提供崩溃的堆栈跟踪。
标签: java android android-sqlite