![](/img/trans.png)
[英]Android - How to Store HashMap with Custom Key (and custom data) to internal storage?
[英]How to store JPEG file path from internal storage to SQLite database & what data type to store it as?
我正在尝试将内部存储中的图像显示到我的回收站视图中,我已使用convertToBitmap ()
将图像资源制作为位图,并使用storeToFile ()
和saveBitmap ()
将它们作为 jpeg 文件保存在共享存储中。
但是,我想将 jpeg 文件路径存储到我的 SQLite 数据库中,并在回收器视图适配器中检索它们。 问题是我不知道如何在 SQLite 数据库中存储 jpeg 文件路径以及我将用于列的数据类型。
资源转bitmap的方法:
public void convertToBitmap () {
faan = BitmapFactory.decodeResource(context.getResources(), R.drawable.faan);
}
bitmap在共享存储中存储为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");
}
有两种选择:如果您有一些属于相同对象的图像,您可以将路径保存为字符串列中,以逗号分隔(“first_path,second_path,third_path,...”)
当您检索该值时,您可以使用 myString.split(",") 来获取字符串数组中的路径
如果每个图像属于不同的 object,则必须为路径创建一个字符串列,但每个路径将位于属于数据库中相应 object 的不同行中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.