问题描述
我正在尝试将内部存储器中的图像显示到回收器视图中,我已经使用convertToBitmap ()
将图像资源制作为位图,并使用storeToFile ()
将它们保存为jpeg文件在共享存储中,并且saveBitmap ()
。
但是,我想将jpeg文件路径存储到我的SQLite数据库中,并在回收视图适配器中检索它们。问题是我不知道如何将jpeg文件路径存储在SQLite数据库中以及列将使用哪种数据类型。
将资源转换为位图的方法:
public void convertToBitmap () {
faan = BitmapFactory.decodeResource(context.getResources(),R.drawable.faan);
}
将位图以JPG格式存储在共享存储中的方法:
public void saveBitmap (Bitmap bitmap,String name) {
File filePath = Environment.getExternalStorageDirectory();
File dir = new File (filePath.getAbsolutePath() + "/Items/");
dir.mkdir();
File file = new File (dir,name + ".jpg");
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e){
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
}
public void storeToFile () {
saveBitmap(faan,"Faan");
}
解决方法
有两种选择: 如果您有一些属于同一对象的图像,则可以将路径另存为以逗号分隔的String列(“ first_path,second_path,third_path,...”)
检索该值时,可以使用myString.split(“,”)获得String数组中的路径
如果每个图像都属于一个不同的对象,则必须为该路径创建一个String列,但是每个路径将位于属于数据库中相应对象的不同行中。