android – 如何在Sugar ORM数据库中保存图像?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了android – 如何在Sugar ORM数据库中保存图像?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2424字,纯文字阅读大概需要4分钟。
内容图文
![android – 如何在Sugar ORM数据库中保存图像?](/upload/InfoBanner/zyjiaocheng/894/d68bfc4aaee449599e51afca33b72cf6.jpg)
我想用糖orm在我的数据库中保存图像.我怎样才能做到这一点?
我有下一个代码来尝试保存它:
实体:
import android.graphics.drawable.Drawable;
import android.media.Image;
import com.orm.SugarRecord;
import com.orm.dsl.NotNull;
import com.orm.dsl.Unique;
public class Exercise extends SugarRecord {
@Unique
protected String name;
@NotNull
protected String description;
protected Drawable image;
protected String video;
public Exercise() {
}
public Exercise(String name, String description, Drawable image, String video) {
this.name = name;
this.description = description;
this.image = image;
this.video = video;
}
/**
* @return String name
*/
public String getName() {
return name;
}
/**
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* @return String description
*/
public String getDescription() {
return description;
}
/**
* @param description
*/
public void setDescription(String description) {
this.description = description;
}
/**
* @return Image image
*/
public Drawable getImage() {
return image;
}
/**
* @param image
*/
public void setImage(Drawable image) {
this.image = image;
}
/**
* @return String video
*/
public String getVideo() {
return video;
}
/**
* @param video
*/
public void setVideo(String video) {
this.video = video;
}
}
我想用下一个代码保存:
Exercise addExercise = new Exercise("Prueba", "Prueba 2", image.getDrawable(),"");
addExercise.save();
变量图像是一个ImageView,我将保存的图像是在图库中拍摄的图像:
if(requestCode == SELECT_PHOTO && resultCode == getActivity().RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
当我尝试保存时出现下一个错误:
Class cannot be read from Sqlite3 database. Please check the type of
field image(android.graphics.drawable.Drawable)
谢谢!
解决方法:
试试这样吧
限定
protected byte[] image;
在Exercise类中,还可以在构造函数中更改图像变量的类型.
现在将图像转换为字节数组
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,90,90,stream);
Byte[] image_byte=stream.toByteArray();
实际上将整个图像存储在DB中是基于读/写效率的坏主意.最好将图像存储在单独的文件夹中并将路径存储为数据库中的字符串(varchar)
内容总结
以上是互联网集市为您收集整理的android – 如何在Sugar ORM数据库中保存图像?全部内容,希望文章能够帮你解决android – 如何在Sugar ORM数据库中保存图像?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。