将图像保存在空文件夹中:Android

时间:2014-01-13 07:23:30

标签: android

String gender;   


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
    mTextureView = (TextureView) findViewById(R.id.textureView1);
    mTextureView.setSurfaceTextureListener(this);


    final Button buttonStartCameraPreview = (Button)findViewById(R.id.startcamerapreview);
    final Button buttonCapturePreview = (Button)findViewById(R.id.Capturecamerapreview);

    rgGender = (RadioGroup) findViewById(R.id.rgGender);
    rdbMale = (RadioButton) findViewById(R.id.rdbMale);
    rdbFemale = (RadioButton) findViewById(R.id.rdbFemale);


    PatientInfo = (EditText) findViewById(R.id.PatientName);
    PatientInfo.setHint("Enter patient name");
    PatientAge = (EditText) findViewById(R.id.Age);
    PatientAge.setHint("Age");
    PatientId = (EditText) findViewById(R.id.PatientId);
    PatientId.setHint("Enter patient Id no:");



    rawCallback = new PictureCallback() 
    {
        public void onPictureTaken(byte[] data, Camera camera)
        {
            Log.d("Log", "onPictureTaken - raw");
        }
    };

    shutterCallback = new ShutterCallback()
    {
        public void onShutter() {
            Log.i("Log", "onShutter'd");
        }
    };

    jpegCallback = new PictureCallback()
    {

        @Override
        public void onPictureTaken(byte[] data, Camera mCamera) 
        {
            int imageNum = 0;
            String Name = PatientInfo.getText().toString();
            String Age =  PatientAge.getText().toString();
            String Id = PatientId.getText().toString();

            Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            Date d = new Date();
            CharSequence s  = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());

            Rname = "Raw"+s.toString() + "_img_"+ String.valueOf(imageNum)+".jpg";
            Pname = "Proc"+s.toString()+ "_img_"+ String.valueOf(imageNum) +".jpg";

            File imagesFolders = new File(Environment.getExternalStorageDirectory().toString() + "/" + Name + Age + gender +Id);



            imagesFolders.mkdirs();
            File output = new File(imagesFolders, Rname);

            while (output.exists()){
                imageNum++;
                Rname = "Raw"+s.toString() + "_img_"+ String.valueOf(imageNum) +".jpg";
                Pname = "Proc"+s.toString() + "_img_"+ String.valueOf(imageNum) +".jpg";
                output = new File(imagesFolders, Rname);
            }

            callname ="/sdcard/"+ Name + Age + gender + Id +"/"+ Rname;
            Uri uriSavedImage = Uri.fromFile(output);
            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
            OutputStream imageFileOS;

            try {

                imageFileOS = getContentResolver().openOutputStream(uriSavedImage);
                imageFileOS.write(data);
                imageFileOS.flush();
                imageFileOS.close();        } 
            catch (FileNotFoundException e) 
                {
                e.printStackTrace();
                } 
            catch (IOException e)
            {
                e.printStackTrace();

            } 
            finally
            {}
            Log.d("Log", "onPictureTaken - jpeg");
       }

             public void onRadioButtonClicked(View view) {

        boolean checked = ((RadioButton) view).isChecked();
        switch(view.getId()) 
        {
            case R.id.rdbMale:
                if (checked)
                    gender= "M";
                rdbFemale.setChecked(false);

                break;
            case R.id.rdbFemale:
                if (checked)
                    gender = "F";

                rdbMale.setChecked(false);

                break;
        }
        }

这是一段代码。在输入名称,年龄,性别等详细信息后,我试图将文件保存在文件夹中。但如果我没有输入任何东西,只是拍摄图像,它会将图像保存到空文件夹中。我不希望这种情况发生。如果未输入详细信息,则不应将图像保存在任何位置,并且应显示消息,请输入数据。任何人都可以帮助为什么它将数据保存为空文件夹

1 个答案:

答案 0 :(得分:3)

使用验证程序。

if (edt.getText().toString().length() <= 0) {
    edt.setError("Please Enter Name/Name Required.");
    valid_name = null;
} else if (!edt.getText().toString().matches("[a-zA-Z ]+")) {
    edt.setError("Accept Alphabets Only.");
    valid_name = null;
} else {
    valid_name = edt.getText().toString();
}

或参考此网站: http://chintankhetiya.wordpress.com/2013/05/29/edit-text-validation/