So here is what I am struggling with at the moment. I am saving the image taken by the camera to my SQLite database by using this code:
DatabaseDB:
@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_IMG + " text " +
" ) ";
db.execSQL(sql);
}
Then in the camera intent (Main Activity)
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent getCameraImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"MapImages/");
else
cameraFolder= context.getFilesDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File photo = new File(cameraFolder, "MapImages/" + imageFileName + ".jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
Uri.fromFile(photo);
startActivityForResult(getCameraImage, TAKE_PICTURE);
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, mMap.getCameraPosition().zoom);
contentValues.put(FIELD_IMG, String.valueOf(photo));
LocationInsertTask insertTask = new LocationInsertTask();
insertTask.execute(contentValues);
So when I look in the database it save the image file location as:
/storage/emulated/0/MapImages/MapImages/JPEG_20211105_091000_.jpg
So that all works great! But how do I now go about retrieving that image and displaying it in a ImageView
?
I have tried this (in the onloadFinished)
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
int locationCount = 0;
double lat = 0;
double lng = 0;
float zoom = 0;
double 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.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_IMG));
LatLng location = new LatLng(lat, lng);
drawMarker(location);
arg1.moveToNext();
}
if (locationCount > 0) {
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lng)));
mMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));
Bitmap myBitmap = BitmapFactory.decodeFile(String.valueOf(img));
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
}
}
But then receives the following error:
E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: 0.0: open failed: ENOENT (No such file or directory)
I do not want to save the image as a blob
as it is bad practice hence why I am saving the file path to the DB
If someone can please assist in guiding me to retrieve the file from the DB and display in my ImageView
Thanks everyone!
Replace:
img = arg1.getDouble(arg1.getColumnIndex(LocationsDB.FIELD_IMG));
with:
img = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMG));
This will also require you to declare img
as a String
.
A filesystem path is a string, not a floating-point number.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.