In my app the user can take an image via the camera intent:
byte[] imageData;
ByteArrayOutputStream baos;
.....
@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_ZOOM, googleMap.getCameraPosition().zoom);
contentValues.put(LocationsDB.FIELD_IMAGE, imageData);
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(), "Kruger National Park");
imagesFolder.mkdirs();
image = new File(imagesFolder.getPath(), "MySighting_" + 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();
}
Then in onActivityResult
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.JPEG, 100, baos);
imageData = baos.toByteArray();
myMarkersHash.put(markerId, bitmap);
}
}
i then converted the bitmap to byte array (as seen in above code)
Then also in the Camera Intent code you'll see that i have added contentValues.put(LocationsDB.FIELD_IMAGE, imageData);
And the in my LocationsDB class i added the column:
public static final String FIELD_IMAGE = "img";
....
@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 " +
" ) ";
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);
}
and then in my MainActivity class again i added this to load the image again:
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat=0;
double lng=0;
float zoom=0;
float img=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));
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));
imageData = baos.toByteArray();
}
But the problem is when launching the app the crashes at line 336 which is imageData = baos.toByteArray(); with a nullPointerException
What am i doing wrong here?
Aucun commentaire:
Enregistrer un commentaire