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

标签 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");
        }
    };

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

最佳答案

改变

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

带文件

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

请确保在 list 中也有 WRITE_EXTERNAL_PERMISSION,并且 Name 不为空

关于android - 使用用户输入 : Android 创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20283930/

相关文章:

java - 无法从 View 转换为按钮错误

javascript - 如何退出 Android 版 Phonegap 应用程序?

android - 从SD卡加载声音时出现问题

Android TextChangedListener 和 OnKeyListener 干扰

android - 双向数据绑定(bind)如何导致无限循环?

java - 如何在 GridView 中使项目透明

android - 使用 ngCordova-oauth 在 Ionic App 中进行社交身份验证

android - 尝试对自定义对象数组执行 readParcelable 时发生 ClassCastException

java - 如何重用一个 Intent 并将其带到最前面?

android - fragment 被销毁后停止处理程序