lundi 22 décembre 2014

How can I save the markerId() and reference to image for specific marker in SQlite Database?

I need to save the the markerId() as well as the image in the SQLite database so when reloading the app the image is displayed for that marker.


Here is my code for placing the marker as well as getting the image for that marker:



@Override
public void onMapLongClick(LatLng point) {
thePoint=point;

drawMarker(point);
ContentValues contentValues = new ContentValues();
contentValues.put(LocationsDB.FIELD_LAT, point.latitude );
contentValues.put(LocationsDB.FIELD_LNG, point.longitude);
contentValues.put(LocationsDB.FIELD_IMAGE, bitmap); //error under "put" The method put(String, String) in the type ContentValues is not applicable for the arguments (String, Bitmap)
contentValues.put(LocationsDB.FIELD_MARKER_ID, markerId);
contentValues.put(LocationsDB.FIELD_ZOOM, googleMap.getCameraPosition().zoom);
LocationInsertTask insertTask = new LocationInsertTask();
insertTask.execute(contentValues);
Toast.makeText(getBaseContext(), "Please Take Photo", Toast.LENGTH_SHORT).show();

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 Folder");
imagesFolder.mkdirs();
image = new File(imagesFolder.getPath(), "Img_" + 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();
marker.getPosition();

}


Then in my database if have:



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 = "img";
public static final String FIELD_MARKER_ID = "id";
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 + " text , " +
FIELD_MARKER_ID + "text " +
" ) ";

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, FIELD_MARKER_ID } , null, null, null, null, null, null);
}


Then in my onLoadFinished (this is where i don't really know what to put)



public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
float zoom=0;
float img=0;
float id=0;
locationCount = arg1.getCount();
arg1.moveToFirst();

for(int i=0;i<locationCount;i++){

lat = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LAT));
lng = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_LNG));
zoom = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_ZOOM));
img = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
id = arg1.getFloat(arg1.getColumnIndex(LocationsDB.FIELD_MARKER_ID));
thePoint = new LatLng(lat, lng);
drawMarker(thePoint);
arg1.moveToNext();
}

if(locationCount>0){
googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat,lng)));
googleMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
float bitmap = (img);
float markerId = (id);
}
}


And this is the HashMap for the markers added:



private Map<String, Bitmap> myMarkersHash;
private String markerId;

....

myMarkersHash = new HashMap<String, Bitmap>();

....

final ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
Bitmap bitmap = myMarkersHash.get(marker.getId());
markerIcon.setImageBitmap(bitmap);


private void drawMarker(LatLng point) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.position(thePoint));
markerId = marker.getId();


Please be kind to me, this is my first time using SQlite and still learning :-) Thanks


Aucun commentaire:

Enregistrer un commentaire