简体   繁体   English

令牌“ mkdirs”上的语法错误,此令牌后应有标识符

[英]Syntax error on token “mkdirs”, Identifier expected after this token

I'm attempting the create a directory programmaticly to build out the Android Photo Capture app here . 我正在尝试以编程方式创建目录以在此处构建Android Photo Capture应用。 I tried creating a class to create the directory before define the path, but Eclipse is throwing the error Syntax error on token "mkdirs", Identifier expected after this token. 我尝试在定义路径之前创建一个类来创建目录,但是Eclipse Syntax error on token "mkdirs", Identifier expected after this token.抛出错误Syntax error on token "mkdirs", Identifier expected after this token. Should I be running the mkdirs() code elsewhere in the file? 我应该在文件的其他位置运行mkdirs()代码吗?

    public class PhotoCaptureExample extends Activity 
    {
         public class Dir
         {
        File folder = new File(Environment.getExternalStorageDirectory().toString()+"/DrawIn");
        folder.mkdirs();

         }

         protected Button _button;

Thank you! 谢谢!

You have code outside of any function. 您有任何功能之外的代码。 That isn't legal, except for simple initializers. 除了简单的初始化程序外,这是不合法的。

to create a directory, you could use 创建目录,您可以使用

File myDirectory = new File(Environment.getExternalStorageDirectory(), "DrawIn");

if(!myDirectory.exists()) {                                 
  myDirectory.mkdirs();
}

don't forget to add permission: 不要忘记添加权限:

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM