Hello and hi to everyone who read this. From my question above, currently, I create a system that requires the user to snap a picture and click button save. After that, on the same activity, there's a button called "Next", which is to move to the next activity.
My problem is, I don't know how to prevent the user from going to the next activity if the user didn't snap a photo and save it to the MySQL database. Below is my code
TaskUpdateBefore.JAVA
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_task_update_before);
setTitle("Task Details - Before");
if (!SharedPrefManager.getInstance(this).isLoggedIn()) {
finish();
startActivity(new Intent(this, MainActivity.class));
}
taskClass = (TaskClass) Objects.requireNonNull(getIntent().getExtras()).getSerializable("task");
//before
btnCameraBefore = findViewById(R.id.btnCameraBefore);
imgAttachBefore = findViewById(R.id.imgAttachBefore);
btnSaveBefore = findViewById(R.id.btnSaveBefore);
tvTaskName = findViewById(R.id.tvTaskName);
tvTaskName.setText("Task: "+taskClass.getTask_name());
btnNext = findViewById(R.id.btnNext);
imgAttachBefore.setImageBitmap(base64ToBitmap(taskClass.getPhoto_before()));
EnableRuntimePermission();
btnCameraBefore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
btnSaveBefore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
photoBefore();
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(TaskUpdateBefore.this, TaskUpdateAfter.class);
intent.putExtra("task", taskClass);
startActivity(intent);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 7 && resultCode == RESULT_OK) {
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imgAttachBefore.setImageBitmap(bitmap);
}
}
public void EnableRuntimePermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(TaskUpdateBefore.this,
Manifest.permission.CAMERA))
{
Toast.makeText(TaskUpdateBefore.this,"CAMERA permission allows us to Access CAMERA app", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(TaskUpdateBefore.this,new String[]{
Manifest.permission.CAMERA}, RequestPermissionCode);
}
}
@Override
public void onRequestPermissionsResult(int RC, String per[], int[] PResult) {
switch (RC) {
case RequestPermissionCode:
if (PResult.length > 0 && PResult[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(TaskUpdateBefore.this,"Permission Granted, Now your application can access CAMERA.", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(TaskUpdateBefore.this,"Permission Canceled, Now your application cannot access CAMERA.", Toast.LENGTH_LONG).show();
}
break;
}
}
private void photoBefore() {
BitmapDrawable drawable = (BitmapDrawable) imgAttachBefore.getDrawable();
photo_before = "";
try {
photo_before = bitmapToBase64(drawable.getBitmap());
}catch (Exception e){
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Save photo");
builder.setMessage("Do you want to save this photo?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
saveBefore(photo_before);
Intent intent = new Intent(TaskUpdateBefore.this, TaskList.class);
startActivity(intent);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.show();
}
public static String bitmapToBase64(Bitmap image) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.PNG, 100, os);
byte[] byteArray = os.toByteArray();
String encodedImageString = Base64.encodeToString(byteArray, Base64.DEFAULT);
return encodedImageString ;
}
public static Bitmap base64ToBitmap(String encodedString) {
byte[] decodedString = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(decodedString , 0,
decodedString.length);
return bitmap;
}
private void saveBefore(String photo_before) {
class savePhotoBefore extends AsyncTask<String, Void, String> {
ProgressDialog loading;
RequestHandler requestHandler = new RequestHandler();
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<String, String>();
data.put("photo_before", params[0]);
String result = requestHandler.sendPostRequest(URLs.URL_UPDATE_BEFORE +"?report_id="+ taskClass.getReport_id(), data);
return result;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(TaskUpdateBefore.this, "Saving..", null, true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
savePhotoBefore sl1 = new savePhotoBefore();
sl1.execute(photo_before);
}
@Override
public void onBackPressed() {
Intent intent = new Intent(TaskUpdateBefore.this, TaskList.class);
startActivity(intent);
}
task_update_before.php
<?php
require_once "config.php";
$photo_before = $_POST['photo_before'];
$report_id = $_GET["report_id"] ?? "";
$sql_query = "UPDATE report SET photo_before ='$photo_before', time_photo_before = NOW(), ot_start = '16:00:00' WHERE report_id = '$report_id'";
if(mysqli_query($conn,$sql_query))
{
echo "Data Save!";
}
else
{
echo "Error!! Not Saved".mysqli_error($conn);
}
?>