我正在尝试将我的图像保存到SQLite数据库,并且现在已经挣扎了一段时间。因此,经过几周的研究,我终于决定检查图像是否实际添加到数据库而不是。我设法显示我的数据库,可以看到lat和lng点以及缩放级别保存到数据库但不保存到图像(它在“blob”字段中为null)
所以这就是我所拥有的:
对于我的数据库,我有这个:
public class LocationsDB extends SQLiteOpenHelper{
private static String DBNAME = "locationmarkersqlite";
private static int VERSION = 1;
public static final String FIELD_ROW_ID = "_id";
public static final String FIELD_LAT = "lat";
public static final String FIELD_LNG = "lng";
public static final String FIELD_ZOOM = "zom";
public static final String FIELD_IMAGE = "blob";
private static final String DATABASE_TABLE = "locations";
private SQLiteDatabase mDB;
public LocationsDB(Context context) {
super(context, DBNAME, null, VERSION);
this.mDB = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table " + DATABASE_TABLE + " ( " +
FIELD_ROW_ID + " integer primary key autoincrement , " +
FIELD_LNG + " double , " +
FIELD_LAT + " double , " +
FIELD_ZOOM + " text , " +
FIELD_IMAGE + " blob " +
" ) ";
db.execSQL(sql);
}
public long insert(ContentValues contentValues){
long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
return rowID;
}
public int del(){
int cnt = mDB.delete(DATABASE_TABLE, null , null);
return cnt;
}
public Cursor getAllLocations(){
return mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID, FIELD_LAT , FIELD_LNG, FIELD_ZOOM, FIELD_IMAGE} , null, null, null, null, null, null);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public static Cursor rawQuery(String string, Object object) {
// TODO Auto-generated method stub
return null;
}
然后在我的LocationsContentProvider
课程中我有这个:
public class LocationsContentProvider extends ContentProvider{
public static final String PROVIDER_NAME = "com.test.markertest.locations";
public static final Uri CONTENT_URI = Uri.parse("content://" + PROVIDER_NAME + "/locations" );
private static final int LOCATIONS = 1;
private static final UriMatcher uriMatcher ;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "locations", LOCATIONS);
}
LocationsDB mLocationsDB;
@Override
public boolean onCreate() {
mLocationsDB = new LocationsDB(getContext());
return true;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = mLocationsDB.insert(values);
Uri _uri=null;
if(rowID>0){
_uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
}else {
try {
throw new SQLException("Failed to insert : " + uri);
} catch (SQLException e) {
e.printStackTrace();
}
}
return _uri;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
return 0;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int cnt = 0;
cnt = mLocationsDB.del();
return cnt;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
if(uriMatcher.match(uri)==LOCATIONS){
return mLocationsDB.getAllLocations();
}
return null;
}
@Override
public String getType(Uri uri) {
return null;
}
最后在我的MainActivity
课程中我完成了这个:
@Override
public void onMapLongClick(LatLng point) {
thePoint=point;
drawMarker(point);
contentValues.put(LocationsDB.FIELD_LAT, point.latitude);
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
LocationInsertTask insertTask = new LocationInsertTask();
insertTask.execute(contentValues);
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My Images");
imagesFolder.mkdirs();
image = new File(imagesFolder.getPath(), "Myimage_" + timeStamp + ".jpg");
fileUri = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(imageIntent, TAKE_PICTURE);
}
private void drawMarker(LatLng point) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(thePoint));
markerId = marker.getId();
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
try {
GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
bitmap = getImageThumbnail.getThumbnail(fileUri, this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
ExifInterface exif = null;
try {
exif = new ExifInterface(image.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
if (orientation != -1) {
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotate = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotate = 270;
break;
default:
rotate = 0;
break;
}
}
Matrix matrix = new Matrix();
matrix.postRotate(rotate);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);
baos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 100, baos);
imageData = baos.toByteArray();
myMarkersHash.put(markerId, bitmap);
contentValues.put(LocationsDB.FIELD_IMAGE, markerId + imageData);
}
}
所以我试过将此contentValues.put(LocationsDB.FIELD_IMAGE, markerId + imageData);
更改为此contentValues.put(LocationsDB.FIELD_IMAGE, imageData);
但仍然没有运气。数据库仍然在FIELD_IMAGE
列中说明null
有谁能告诉我我做错了什么? 感谢
答案 0 :(得分:0)
LocationInsertTask
在图像捕获活动开始之前开始执行,因此blob数据被放入ContentValues
方式太晚了。
在获得活动结果后运行插入。