使用用户输入创建文件夹:Android

时间:2013-11-29 10:47:08

标签: android

jpegCallback = new PictureCallback()
        {
        public void onPictureTaken(byte[] data, Camera camera) 
         {
            //int imageNum = 0;

            String Name = PatientInfo.getText().toString();

            Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            File imagesFolder = new File(Environment.getExternalStorageDirectory().toString() + Name);
            imagesFolder.mkdirs(); 
            Date d = new Date();
            CharSequence s  = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());
            File output = new File(imagesFolder, s.toString() + ".jpg");
          //  String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
         //   File output = new File(imagesFolder, fileName);
       /*     while (output.exists()){
                imageNum++;
                fileName = "image_" + String.valueOf(imageNum) + ".jpg";
                output = new File(imagesFolder, fileName);
            }*/
            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();

                Toast.makeText(AndroidCamera.this, 
                        "Image saved: ", 
                        Toast.LENGTH_LONG).show();

                } 
            catch (FileNotFoundException e) 
                {
                e.printStackTrace();
                } 
            catch (IOException e)
            {
                e.printStackTrace();
            } 
            finally
            {}
            Log.d("Log", "onPictureTaken - jpeg");
        }
    };

这是一个代码示例。我正在尝试在输入时使用userinput创建文件夹。每次我需要在输入新用户名时创建新文件夹。但是在这方面有一些缺失。任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:2)

更改

File imagesFolder = new File(Environment.getExternalStorageDirectory().toString() + Name);

withFile

File imagesFolder = new File(Environment.getExternalStorageDirectory(),  Name);

请确保清单中还有WRITE_EXTERNAL_PERMISSION,且Name不为空