我有高低搜索,阅读许多不同的帖子和教程,但我似乎无法弄清楚如何将我的图像保存在数据库中。
似乎每当我尝试将图像放入数据库时,都会导致数据库无法创建,然后我会关闭一个强制关闭。我很乐意帮忙。
这是我的代码
package com.ondrovic.boombozzpassport;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.provider.BaseColumns;
import android.util.Log;
public class Database extends SQLiteOpenHelper implements BaseColumns {
private final static String DB_NAME = "boombozz.db";
private final static int DB_VERSION = 1;
static final String TABLE_BEERS = "beers";
static final String COL_NAME = "name";
static final String COL_BREWER = "brewer";
static final String COL_ABV = "abv";
static final String COL_RATE = "rating";
static final String COL_BDESC = "breifdescription";
static final String COL_FDESC = "fulldescription";
static final String COL_TYPE = "type";
static final String COL_PIC = "picture";
private Context mContext;
private Bitmap picture = null;
public Database(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE beers (" + "_id INTEGER PRIMARY KEY, "
+ "name TEXT, " + "brewer TEXT, " + "abv REAL, "
+ "rating REAL, " + "breifdescription TEXT, "
+ "fulldescription TEXT, " + "type TEXT, " + "picture BLOB);");
addBeer(db, "NAME 1", "BREWER 1", "TYPE 1", "BDESC 1", "FDESC 1", 0, 0, R.drawable.beer1);
}
private void addBeer(SQLiteDatabase db, String name, String brewer,
String type, String bdesc, String fdesc, int abv, int rate, int image) {
final ContentValues cv = new ContentValues();
cv.put(COL_NAME, name);
cv.put(COL_BREWER, brewer);
cv.put(COL_TYPE, type);
cv.put(COL_BDESC, bdesc);
cv.put(COL_FDESC, fdesc);
cv.put(COL_ABV, abv);
cv.put(COL_RATE, rate);
final Bitmap bitmap = BitmapFactory.decodeResource(
mContext.getResources(), image);
writeBitmap(cv, COL_PIC, bitmap);
db.insert(TABLE_BEERS, null, cv);
}
static void writeBitmap(ContentValues cv, String name, Bitmap image) {
if (image != null) {
try {
int size = image.getWidth() * image.getHeight() * 2;
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
image.compress(CompressFormat.PNG, 100, out);
out.flush();
out.close();
cv.put(name, out.toByteArray());
} catch (IOException e) {
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w("onUpgrade", "Upgrading database from version: " + oldVersion
+ " to version: " + newVersion);
db.execSQL("DROP TABLE IF EXISTS beers");
}
}
答案 0 :(得分:1)
你在下载这些图片吗?如果是,请按照Venkata Krishna的建议将其保存到SD卡/手机记忆库。将图像保存到磁盘中非常简单。
首先,我们创建一个方法来检查我们是否可以读写外部存储磁盘。
/**
* @return true if the external storage is mounted or read only.
*/
public static boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state);
}
/**
* @return true if the external storage is mounted and writable.
*/
public static boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
return Environment.MEDIA_MOUNTED.equals(state);
}
然后,另一种方法将检查外部存储磁盘中是否有足够的可用空间。
/**
* @return the amount of free space on the external storage.
*/
public static long getAvailableExternalMemorySize() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return availableBlocks * blockSize;
}
现在我们创建一个方法,告诉我们是否可以将文件存储在SD卡中。
/**
* @return true if the external storage is available, writable,
* and contains enough free space.
*/
public static boolean isExternalStorageAvailable() {
if (!isExternalStorageWritable()) return false;
long availableSize = getAvailableExternalMemorySize();
if (availableSize < REQUIRED_STORAGE_SPACE) {
return false;
}
return true;
}
REQUIRED_STORAGE_SPACE
是一个常量,其中包含您的应用用于存储图像和其他内容的空间量。
现在我们将创建一个存储图像的方法。
/**
* @return the File from the given filename.
*/
public static File getImageFile(String filename) {
if (!isExternalStorageReadable()) return null;
// The images folder path.
String imagesFolder = Environment.getExternalStorageDirectory().getPath()
+ "Android/data/your.app.package/images/";
// Creating the file.
File file = new File(imagesFolder + filename);
return file;
}
/**
* Write the contents of the HTTP entity to the external
* storage if available and writable.
*/
public static boolean storeImage(HttpEntity entity, String filename) throws IOException {
if (isExternalStorageAvailable()) {
File file = getImageFile(filename);
if (file == null) return false;
// Write to file output stream.
FileOutputStream os = new FileOutputStream(file);
entity.writeTo(os);
os.close();
return true;
}
return false;
}
将所有这些方法放入一个类中,比方说ImageHelper
,检查您的清单文件中是否有权限写入外部存储,然后您就可以了。
答案 1 :(得分:0)
将图像保存在SD卡或手机内存中,并将保存的图像路径存储在数据库中。每当您想要访问图像时,请从数据库中获取图像路径并使用该路径访问图像。