【发布时间】:2016-01-18 08:36:12
【问题描述】:
我正在开发的 Android 应用程序基本上是一个购物应用程序,它有一个购物车选项,用户可以在其中添加商品。
商品有图片、名称、价格等。
我从服务器获取所有这些数据。
当用户单击“添加到购物车”选项时,会创建一个 sqlite 数据库,用于存储名称、价格和图像路径。
当点击添加到购物车时,基本上图像存储在内存中,数据库中只存储图像路径。
问题:
为了将图像存储在内部存储器中,我使用下面的代码,我将自己给出文件名(在这种情况下,我将文件名命名为 profile.jpg)。
保存到内存:
private String saveToInternalSorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
mypath=new File(directory,"profile.jpg");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
从内存加载:
private Bitmap loadImageFromStorage(String path)
{
try {
File f=new File(path, "");
f.canRead();
b = BitmapFactory.decodeStream(new FileInputStream(f));
return b;
}
如果我这样做,最新的图像会被之前的图像覆盖。我无法在内存中存储多张图像。
例如,如果我在购物车中添加两个项目,我不知道如何存储这两个图像并获取它。
瞄准
需要在内存中存储任意数量的随机文件名的图像
将文件名存储在 sqlite 数据库中。
将其取回以在 Imageview 中显示。
任何帮助将不胜感激。
【问题讨论】: