图像到可绘制的数据库
我有搜索喜和低,阅读许多不同的帖子和教程,但我似乎无法弄清楚如何让我的图像保存在数据库中。图像到可绘制的数据库
似乎每次我尝试将图像放入数据库时,都会导致数据库无法创建,然后我会收到一个强制关闭。我会喜欢一些帮助。
这里是我的代码
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");
}
}
你在下载这些图片吗?如果是,请按照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
,检查是否有权限在你的清单文件写入到外部存储,你是好去。
保存在SD卡或手机内存和存储即保存的图像路径database.whenever您要访问的图像,然后从数据库中取图片路径图像,并使用该路径访问图像。
目前我有可绘制文件夹中的图像,但仍然没有运气 – ondrovic 2012-03-07 04:27:00
从Drawable获取图像使用Drawable d = getResources()。getDrawable();并将此Drawable转换为Bitmap.store,该位图在SD卡或手机内存中。 – 2012-03-07 04:30:37
我没有下载图像。有现有的图像我已经有 – ondrovic 2012-03-07 15:44:11
因此,用它们作为@Venkata Krishna建议的可绘制文件夹。你仍然有这个问题吗? – 2012-03-07 18:24:10
是的,有点。我的代码示例错了吗?我错过了什么?谢谢大家 – ondrovic 2012-03-07 19:12:25