android Room获取blob图像并显示
我正在使用Room持久库以Blob格式存储图像android Room获取blob图像并显示,android,database,image,blob,android-room,Android,Database,Image,Blob,Android Room,我正在使用Room持久库以Blob格式存储图像 @Entity(tableName = AppConstants.TABLE_OBSERVATIONS_IMAGE) public class ImageModel { @ColumnInfo(typeAffinity = ColumnInfo.BLOB) public byte[] image; public ImageModel() { } @Ignore public ImageModel
@Entity(tableName = AppConstants.TABLE_OBSERVATIONS_IMAGE)
public class ImageModel {
@ColumnInfo(typeAffinity = ColumnInfo.BLOB)
public byte[] image;
public ImageModel() {
}
@Ignore
public ImageModel( byte[] image ) {
this.image = image;
}
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}
现在我想获取它并在图像视图中显示它。但这是一个例外。我使用以下代码显示图像,其中model.getImage()是我存储的图像。但它返回空位图并导致应用程序崩溃
ImageModel model = observationModelArrayList.get(position).getImageModels().get(i);
ImageView image = new ImageView(context);
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(100, 100));
image.setMaxHeight(100);
image.setMaxWidth(100);
Bitmap bmp = BitmapFactory.decodeByteArray(model.getImage(), 0, model.getImage().length);
//ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(), image.getHeight(), false));
// Adds the view to the layout
holder.llayImages.addView(image);
请提供帮助。您可以将图像存储在内部存储器中,因为加载您可以使用的图像不需要存储权限
private void loadImage(String path){
try {
File file = new File(path, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(file));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b); // it will display the image in imageview
String file_path = saveToInternalStorage(b); // store this file_path in db
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
要从服务器加载映像,可以像这样使用glide库
GlideApp.with(context)
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
String file_path = saveToInternalStorage(resource); // store the bitmap
// save this file_path to your db
}
});
要加载位图,可以使用glide从glide获取位图,并使用save方法存储位图。将图像文件保存在内部存储器中后,请在数据库中存储路径,希望它能帮助您……您可以将图像存储在内部存储器中,因为它不需要存储权限即可加载您可以使用的图像
private void loadImage(String path){
try {
File file = new File(path, "profile.jpg");
Bitmap b = BitmapFactory.decodeStream(new FileInputStream(file));
ImageView img=(ImageView)findViewById(R.id.imgPicker);
img.setImageBitmap(b); // it will display the image in imageview
String file_path = saveToInternalStorage(b); // store this file_path in db
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
要从服务器加载映像,可以像这样使用glide库
GlideApp.with(context)
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
@Override public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
String file_path = saveToInternalStorage(resource); // store the bitmap
// save this file_path to your db
}
});
要加载位图,可以使用glide从glide获取位图,并使用save方法存储位图。将图像文件保存到内部存储器后,请在数据库中存储路径,希望它能帮助您……检查BLOB是否正确存储在db中。顺便说一句,BLOB有1MB的限制,可能是更大的图像大小也可能是一个问题。我可以看到字节数组格式的图像。让我尝试一些较小的图像。我建议将图像存储在文件存储器中,并在数据库中跟踪它的路径。@JeelVankhede是的。我会按照你的建议做的。但在此之前,我正在寻找更多的解决方案。检查BLOB是否正确存储在db中。顺便说一句,BLOB有1MB的限制,可能是更大的图像大小也可能是一个问题。我可以看到字节数组格式的图像。让我尝试一些较小的图像。我建议将图像存储在文件存储器中,并在数据库中跟踪它的路径。@JeelVankhede是的。我会按照你的建议做的。但在此之前,我正在寻找更多的解决方案。