gpt4 book ai didi

java - 从可绘制图像到数据库

转载 作者:行者123 更新时间:2023-12-01 15:30:22 26 4
gpt4 key购买 nike

我搜索过很多不同的帖子和教程,但我似乎不知道如何将我的图像保存在数据库中。

似乎每次我尝试将图像放入数据库时​​,都会导致数据库无法创建,然后我会强制关闭。我希望得到一些帮助。

这是我的代码

 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 的建议将其保存到 SDCard/手机内存中。将图像保存到磁盘中非常简单。

首先我们创建一个方法来检查是否可以读取和写入外部存储磁盘。

/**
* @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,检查 list 文件中是否有权限写入外部存储,然后就可以开始了。

关于java - 从可绘制图像到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9595696/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com