我正在使用Google Vision API进行面部检测。如图所示,我的应用程序通常在没有Google Vision的情况下显示两张图片,但有了它,Google Vision会自动更改这两张图片。
图像存储在drawable文件夹中,通过它们的id(例如,R.drawable.crown_flowers)存储在SQLite数据库中,并在运行时从数据库中获取。
执行面部检测的代码本身不对行为负责。只需拥有
compile 'com.google.android.gms:play-services-vision:9.8.0'
build.gradle文件中的会导致此行为,即使库未在实际代码中的任何位置引用。我尝试过使用更新版本的库(11.8.0),但无济于事。
编辑:做一些调试我发现问题出在SQLite数据库上。如果我仅通过其drawable id引用图片而不从数据库中获取它们,则该应用程序可正常工作。问题是这个应用程序是针对学校项目的,我需要使用SQLite。这是数据库的代码:
从DatabaseConnector类:
public class DatabaseConnector extends SQLiteOpenHelper {
public DatabaseConnector(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public void queryData(String sqlString) {
SQLiteDatabase database = getWritableDatabase();
database.execSQL(sqlString);
}
public void insertData(String name, int imgUrl) {
SQLiteDatabase database = getWritableDatabase();
String sqlString = "INSERT INTO FILTER VALUES (NULL, ?, ?);";
SQLiteStatement statement = database.compileStatement(sqlString);
statement.bindString(1, name);
statement.bindLong(2, imgUrl);
statement.execute();
}
public Cursor getData(String sqlString) {
SQLiteDatabase database = getReadableDatabase();
return database.rawQuery(sqlString, null);
}
}
这是在Activity类中的数据库中插入数据的地方:
private void connectDatabase() {
databaseConnector = new DatabaseConnector(this, "FilterDB.sqlite", null, 1);
databaseConnector.queryData("CREATE TABLE IF NOT EXISTS FILTER (Id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, image INTEGER);");
if (databaseConnector.getData("SELECT * FROM FILTER").getCount() < 2) {
databaseConnector.insertData("Primavera", R.drawable.crown_flowers);
databaseConnector.insertData("Desir", R.drawable.sparkle);
}
}
这是从数据库中提取图像的地方:
Cursor cursor = ((HomepageActivity)mActivity).databaseConnector.getData("SELECT * FROM FILTER");
if (cursor.moveToFirst() && position > 16) {
mFilters.clear();
do {
int id = cursor.getInt(0);
String name = cursor.getString(1);
int imgId = (int)cursor.getLong(2);
mFilters.add(new OverFilter(id, name, imgId));
} while (cursor.moveToNext());
}
为了清楚起见,这是OverFilter类:
public class OverFilter {
private String mName;
private int mImage;
private int mId;
public OverFilter(int mId, String mName, int mImage) {
this.mName = mName;
this.mImage = mImage;
this.mId = mId;
}
public String getName() {
return mName;
}
public void setName(String mName) {
this.mName = mName;
}
public int getImage() {
return mImage;
}
public void setImage(int mImage) {
this.mImage = mImage;
}
public int getId() {
return mId;
}
public void setId(int mId) {
this.mId = mId;
}
}
mFilter只是OverFilter的一个ArrayList。
答案 0 :(得分:0)
对于任何可能在将来遇到同样问题的人,我通过更改数据库中图片的主键ID来解决它。我做了非自动增量,我手动设置为一个数字,似乎不太可能与存储在谷歌视觉库中的图片有任何冲突。 这是更新的代码:
public void insertData(int id, String name, int imgUrl) {
SQLiteDatabase database = getWritableDatabase();
String sqlString = "INSERT INTO FILTER VALUES (?, ?, ?);";
SQLiteStatement statement = database.compileStatement(sqlString);
statement.bindLong(1, id);
statement.bindString(2, name);
statement.bindLong(3, imgUrl);
statement.execute();
}
private void connectDatabase() {
databaseConnector = new DatabaseConnector(this, "FilterDB.sqlite", null, 1);
databaseConnector.queryData("CREATE TABLE IF NOT EXISTS FILTER (id INTEGER PRIMARY KEY, name VARCHAR, image INTEGER);");
if (databaseConnector.getData("SELECT * FROM FILTER").getCount() < 2) {
databaseConnector.insertData(578,"Primavera", R.drawable.crown_flowers);
databaseConnector.insertData(579,"Desir", R.drawable.sparkle);
}
}