首先要注意的是代码在低质量图像下工作得很好,但是当用户使用他们用手机摄像头拍摄的高质量图像时,图像不会显示在应用程序中。然而,较低质量的图像。有人可以解释为什么会发生这种情况以及为此修改代码的最佳方法吗?
ImageDatabaseHandler.java(仅限相关代码)
public Boolean deleteItem(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
String table = "images";
String whereClause = "id=?";
String[] whereArgs = new String[]{String.valueOf(id)};
db.delete(table, whereClause, whereArgs);
return true;
}
public Boolean insertimage(String x, Integer i) {
SQLiteDatabase db = this.getWritableDatabase();
try {
FileInputStream fs = new FileInputStream(x);
byte[] imgbyte = new byte[fs.available()];
fs.read(imgbyte);
ContentValues contentValues = new ContentValues();
contentValues.put("id", i);
contentValues.put("img", imgbyte);
db.insert("images", null, contentValues);
fs.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public Bitmap getimage(Integer id) {
SQLiteDatabase db = this.getWritableDatabase();
Bitmap bt = null;
Cursor cursor = db.rawQuery("select * from images where id=?", new String[]{String.valueOf(id)});
if (cursor.moveToNext()) {
byte[] imag = cursor.getBlob(1);
bt = BitmapFactory.decodeByteArray(imag, 0, imag.length);
}
return bt;
}
MyActivity(仅限相关代码)
int stockNumber = 0;
Button insert, delImg;
EditText number;
ImageView imageView;
ImageDatabaseHandler db;
private static final int PICK_IMAGE = 100;
protected void onCreate(Bundle savedInstanceState) {
....
insert = findViewById(R.id.SetImage);
imageView = findViewById(R.id.imageView);
db = new ImageDatabaseHandler(this);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse(
"content://media/internal/images/media"
));
startActivityForResult(intent, PICK_IMAGE);
}
});
delImg = findViewById(R.id.delImageButton);
delImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startImageRemove();
}
});
}
protected void onStart() {
super.onStart();
try {
if (db.getimage(stockNumber) != null) {
//imageView.setImageBitmap(db.getimage(stockNumber));
imageView.setImageBitmap(Bitmap.createScaledBitmap(db.getimage(stockNumber), 500, 500, false));
}
} catch (NullPointerException e) {
Toast.makeText(getApplicationContext(), "Internal database error!\nset new
image and try again...", Toast.LENGTH_LONG).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri uri = data.getData();
String x = getPath(uri);
db.deleteItem(stockNumber);
finish();
if (db.insertimage(x, stockNumber)) {
Toast.makeText(getApplicationContext(), "Successful", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Not Successful", Toast.LENGTH_SHORT).show();
}
}
}
public String getPath(Uri uri) {
if (uri == null) return null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
在将图像添加到数据库之前,我正在考虑从设备上的媒体商店中选择压缩图像的方法,但是我仍然相对较新的编程和Android Studio,任何帮助或建议都非常感谢,因为总是:)
Sqlite对类型有限制,在你的情况下它可能是BLOB类型。你可以在这里阅读更多关于这些限制的内容:qazxsw poi
更安全的实现方法是将数据库的路径存储到数据库而不是完整的位图。如果您不希望保留原始文件路径,则可以先将映像复制到“应用程序内部文件空间”,然后将新复制的映像位置存储在数据库中。您可以在这里阅读有关Filespace的更多信息:https://www.sqlite.org/limits.html
通过让应用程序启动相机应用程序本身并以这种方式设置图像质量,我能够解决我的问题。请参阅下面的代码;
ImageDatabaseHandler.java(仅限相关代码)
https://developer.android.com/guide/topics/data/data-storage#filesInternal
myActivity.java(仅限相关代码)
public Boolean insertimage(byte[] x, Integer i) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("id", i);
contentValues.put("img", x);
db.insert("images", null, contentValues);
return true;
}