简体   繁体   English

创建新的java.io.File()用于FileDataStoreFactory(DIR)一个新的目录

[英]Creating a new directory with new java.io.File() for FileDataStoreFactory(dir)

My MainActivity looks like the following: 我的MainActivity如下所示:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Button b = (Button) findViewById(R.id.button);
       b.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View v) {
                CalManager c = new CalManager(MainActivity.this);
                try{c.addEvent();}
                catch(IOException e){
                   Log.d("mychecks", "failed");
                }
            }
        });
    }
}

Furthermore, my xml is the following: 此外,我的xml是以下内容:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.user.test">
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>
    <uses-permission 
      android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

And I've got a problem with the following code in the CalManager-class: 而且我已经得到了与在CalManager级以下代码中的问题:

public class CalManager {
    Context ctx;
    String dir;

    public CalManager(Context c){
        this.ctx = c;
        dir = ctx.getFilesDir().toString();
    }

    /** Directory to store user credentials for this application. */
    private static final java.io.File DATA_STORE_DIR = new java.io.File(
            dir , ".credentials/test");

    /** Global instance of the {@link FileDataStoreFactory}. */
    private static FileDataStoreFactory DATA_STORE_FACTORY;

    static {
        try {
            DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    ...
}

The

DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);

Statement throws the following error: 语句引发以下错误:

11-10 09:26:22.939 2374-2374/com.example.user.test W/System.err: java.io.IOException: unable to create directory: /.credentials/test 九月11日至10日:26:22.939 2374年至2374年/ com.example.user.test W / System.err的:产生java.io.IOException:无法创建目录:/.credentials/test

any ideas on how to fix this? 有想法该怎么解决这个吗?

Thanks in advance! 提前致谢!

Creating a new directory with new java.io.File() 使用new java.io.File()创建一个新目录

Stop right there. 停在那儿。 Instantiating a File doesn't create a file on disk. 实例化File不会在磁盘上创建文件。

You need DATA_STORE_DIR.mkdirs() somewhere. 您在某处需要DATA_STORE_DIR.mkdirs()

But you have another problem. 但是你还有另一个问题。 dir is an instance member initialized but he constructor, so you can't use it in static code. dir是实例成员,但已初始化,但他是构造函数,因此您不能在静态代码中使用它。 You wil have to remove static from this File and the anonymous initializer block. 您将不得不从该File和匿名初始化程序块中删除static File

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

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