安卓/MySQL : Prevent to next activity if current activity is empty

标签 android mysql

大家好,大家好!根据我上面的问题,目前,我创建了一个系统,要求用户拍摄照片并单击按钮“保存”。之后,在同一个 Activity 上,有一个名为“Next”的按钮,用于移动到下一个 Activity。

我的问题是,如果用户没有拍照并将其保存到 MySQL 数据库,我不知道如何阻止用户进入下一个 Activity 。下面是我的代码

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);
    }

    ?>

最佳答案

我假设您知道如何拍摄图像并将图像保存到 MYSQL。

为了防止用户转到下一个 Activity ,您可以通过将变量设置为 false 并在保存图像时将变量更改为 true 来验证用户的操作。

单击下一个按钮时,检查变量是 false 还是 true。

boolean is_photo_saved = false;
...
...
public boolean save_photo(image){
#logic    
}
...
...
is_photo_saved = save_photo(image);
...
...
btnNext.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (is_photo_saved){
            Intent intent = new Intent(TaskUpdateBefore.this, TaskUpdateAfter.class);
            intent.putExtra("task", taskClass);
            startActivity(intent);
        } else {
            # Message to user for saving an image first
        }
    }
});

关于安卓/MySQL : Prevent to next activity if current activity is empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59131551/

相关文章:

android - 登录方法没有在android中使用数据绑定(bind)调用

mysql - 或者在列中设置 Null 值

mysql - 如果最终依赖于操作系统,数据库如何保证持久性?

php - 从其他子查询向 mysql 查询添加列

php - MySQL/PHP 确定具有多个值列的表中的最低值

android - StorIO 中可以有哪些列类型?

java - 如何在不显示的情况下使用 Universal ImageLoader 加载图像

Android以编程方式创建选择器

android - 推送通知 - AWS Pinpoint 和 AWS SNS 之间的区别

java - 用于 Android 应用程序的 SQLite、MySQL 或两者