2016-02-17 115 views
1

我正在尝试为我的android项目使用Youtube Data API。我猜API API客户端库Java现在支持Android。我正在关注these samples。在我的Auth.java下线:无法在Youtube上使用FileDataStoreFactory创建目录数据api v3

private static final String CREDENTIALS_DIRECTORY = ".oauth-credentials"; 
File dataDirectory = new File(System.getProperty("user.home") + "/" + CREDENTIALS_DIRECTORY); 
FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(dataDirectory); 

我得到了错误。

unable to create directory: /.oauth-credentials

我试图创建在SD卡中的目录与下面的代码:

Environment.getExternalStorageDirectory().getAbsolutePath() 

,给权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 

但我仍然无法创建目录。库

Additionaly版本我使用:

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    compile 'com.android.support:appcompat-v7:23.1.1' 
    compile 'com.android.support:design:23.1.1' 
    compile 'com.android.support:support-v4:23.1.1' 
    compile 'com.google.apis:google-api-services-youtube:v3-rev162-1.20.0' 
    compile 'com.google.oauth-client:google-oauth-client-java6:1.20.0' 
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.20.0' 
} 

回答

1

试试这个: File dataDirectory = new File(getContext().getFilesDir(), CREDENTIALS_DIRECTORY);

+0

这个代码是如何解答的问题请详细说明。 – JAL

+0

System.getProperty(“user.home”)返回一个空字符串,在android中无用。有错误:无法创建目录:/.oauth-credentials。 http://developer.android.com/reference/java/lang/System.html。 getFilesDir返回应用程序数据目录,您可以将凭据存储在那里 – Aegir