0

I want to load an image from my SQLite database to an imageview when the page loads with the markers that were added previously

To give more explanation, in my app the user can long press on a point and then be able to take a photo and then select save. This the saves to Lat, Lng, zoom and image file path to the SQLite database: (table)

1   31.5836713090539    -25.031741174387    12.0    /storage/emulated/0/Android/data/com.example.app/files/Pictures/JPEG_20211108_114035_8314492593035672521.jpg

So on the page load event I have this:

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    int locationCount = 0;
    double lat = 0;
    double lng = 0;
    float zoom = 0;
    String img = null;

    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.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMG));
        LatLng location = new LatLng(lat, lng);
        imageView.setImageBitmap(BitmapFactory.decodeFile(img));
        drawMarker(location);
        arg1.moveToNext();
    }

    if (locationCount > 0) {
        mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lng)));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(zoom));

    }
}

The data is saved in the button on click event:

 btn_okay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    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(LocationsDB.FIELD_IMG, image.getAbsolutePath());
                    LocationInsertTask insertTask = new LocationInsertTask();
                    insertTask.execute(contentValues);
                    Toast.makeText(getBaseContext(), "Marker is added to the Map", Toast.LENGTH_SHORT).show();
                    alertDialog.dismiss();
                }
            });

The error I am getting when lunching the app after a marker and image has been added is this:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

So I am guessing that the ImageView is located in the Custom Alert Dialog layout and therefore can't find it to display the image once the page loads?

But I am not sure

Thanks

Keith
  • 87
  • 6
  • The `imageView` in `imageView.setImageBitmap(BitmapFactory.decodeFile(img));` code is null; you need to initialize it first – Zain Nov 08 '21 at 10:50

0 Answers0