Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在android中调用camera时,活动中的所有变量都被重新初始化?_Android_Android Camera_Sharedpreferences - Fatal编程技术网

为什么在android中调用camera时,活动中的所有变量都被重新初始化?

为什么在android中调用camera时,活动中的所有变量都被重新初始化?,android,android-camera,sharedpreferences,Android,Android Camera,Sharedpreferences,我是安卓的新手,安卓的摄像头有问题。 在我的类中,我有一些包含一些值的实例变量,还有一个按钮和图像视图。 当我点击该按钮时,应用程序打开相机并存储在sd卡中,然后我对实例变量进行一些操作。 但问题是变量被重新初始化为默认值。 我使用共享首选项或将变量数据存储在另一个类中作为静态变量,这样就可以了。 我的问题是为什么所有实例变量都要重新初始化。 如何克服这个问题 SharedReference,将变量数据作为静态变量存储在另一个类中是唯一的解决方案吗 摄像机代码 public void open

我是安卓的新手,安卓的摄像头有问题。 在我的类中,我有一些包含一些值的实例变量,还有一个按钮和图像视图。 当我点击该按钮时,应用程序打开相机并存储在sd卡中,然后我对实例变量进行一些操作。 但问题是变量被重新初始化为默认值。 我使用共享首选项或将变量数据存储在另一个类中作为静态变量,这样就可以了。 我的问题是为什么所有实例变量都要重新初始化。 如何克服这个问题

SharedReference,将变量数据作为静态变量存储在另一个类中是唯一的解决方案吗

摄像机代码

 public void openCamera() {
    if (Helper.checkCameraHardware(this)) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateFileName = sdf.format(new Date()); // generating
                                                            // todays date
                                                            // for folder

            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
            String curentDateandTime = sdf1.format(new Date()); // generating
                                                                // todays
                                                                // date with
                                                                // min,seconds
                                                                // for image

            // creating an folder in sd card : ed ==> sdCard path/IMG_Folder
            // in helper class / folder with todays date
            File sdImageMainDirectory = new File(Environment
                    .getExternalStorageDirectory().getPath()
                    + "/"
                    + Helper.IMG_FOLDER + "/" + dateFileName);
            if (!sdImageMainDirectory.exists()) { // if folder not exists it
                                                    // created
                sdImageMainDirectory.mkdirs();
            }

            // setting image path to sd card/Img_folder in helper
            // class/todays date folder
            image_PATH = Environment.getExternalStorageDirectory()
                    .getPath()
                    + "/"
                    + Helper.IMG_FOLDER
                    + "/"
                    + dateFileName + "/";
            // creating a new file(jpg) at above image path
            File file = new File(image_PATH, curentDateandTime + ".jpg");

            // re-initilization of image_PATH to new file(jpg)
            image_PATH = image_PATH + curentDateandTime + ".jpg";
            savePreferences();

            // creating an uri for file
            Uri outputFileUri = Uri.fromFile(file);

            Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
            i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(i, 1234);

        } catch (Exception e) {

            Helper.AlertBox(this,
                    "Error No: 001\nPlease Contact us.\n"
                            + e.toString());
        }
    } else {
        Helper.AlertBox(this, "Camera Not Found.!");
    }
}




public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // image_PATH = "";
    image_str = "";

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1234) {
        if (resultCode == RESULT_OK) {

            restorePreferences();

            System.out.println("image_PATH :" + image_PATH);

            File file = new File(image_PATH);
            if (file.exists()) {

                Log.e("File exist :", "File exist at " + image_PATH);

                FileInputStream in;
                BufferedInputStream buf;
                try {
                     in = new FileInputStream(image_PATH);
                     buf = new BufferedInputStream(in);
                     bMap = BitmapFactory.decodeStream(buf);

                     iv_image.setVisibility(View.VISIBLE);
                     iv_image.setImageBitmap(bMap);
                     ConstantClass.image_PATH = image_PATH;

                     if (in != null) {
                     in.close();
                     }
                     if (buf != null) {
                     buf.close();
                     }

                    intent = new Intent(TakePhoto.this, PhotoPreview.class);
                    intent.putExtra("index", Helper.index);
                    Helper.bitMap[Helper.index] = image_PATH;
                    Log.e("test", "1");

                    Helper.btn[Helper.index] = false;
                    Log.e("test", "2");

                    startActivity(intent);
                    Log.e("test", "3");

                    // Helper.AlertBox(this, "Image Captured.");

                } catch (Exception e) {
                    Helper.AlertBox(this,
                            "Error No: 004\nPlease contact Bluefrog technical person.\n"
                                    + e.toString());
                    Log.e("Error reading file", e.toString());
                }

            } else {
                Helper.AlertBox(this,
                        "Error No: 005\nPlease contact Bluefrog technical person.");
            }
        } else {
            Toast.makeText(getApplicationContext(), "Cam Not Supported",
                    5000).show();
        }
    }
}
这里Helper.indexI已作为静态变量存储在某个类中


提前感谢。

日志中是否显示错误?先生,在我的应用程序Activity1调用->Activity2[这有一个按钮和一个图像视图]中,我正在通过intent将一些数据从Activity1传递到Activity2,在Activity2中的Activity2中,我在调用camera之前成功获取数据,如果我调用camera,我从intent获得的数据被重新初始化为默认值。为了避免这种情况,我没有从Activity1i传递数据,我已经将其作为静态变量存储在SharedReference或constant类中,它可以正常工作。我的问题是,这些是唯一的解决方案吗?因此,我假设LogCat中没有显示错误?您不必叫我“先生”,Damien很好:@Damien,因为我使用SharedReference或将其存储在常量类中作为静态变量,所以工作正常。但是,如果我将数据从Activity1传递到Activity2,并在Activity2中调用camera,那么我从Activity1获得的所有数据都将使用默认值重新初始化。在LogCat中,我将获得错误数组索引OutofBounds[由于我从Activity1获得的数据被重新初始化,我通过使用共享首选项或存储在静态变量中解决了这个问题].啊,是的,我现在明白了,很高兴你能找到你所拥有的解决方案。我会思考和研究,因为我无法立即回答这些是否是唯一的解决方案,但它们可能是最好的。