Android getResourceAsStream()返回null
我已经搜索了一段时间,尝试了一些答案,但到目前为止没有任何效果。。 我想通过点击按钮以编程方式创建google日历事件。 为我的MainActivity获取了以下代码:Android getResourceAsStream()返回null,android,google-calendar-api,Android,Google Calendar Api,我已经搜索了一段时间,尝试了一些答案,但到目前为止没有任何效果。。 我想通过点击按钮以编程方式创建google日历事件。 为我的MainActivity获取了以下代码: public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSta
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");
}
}
});
}
}
CalManager类如下所示:
public class CalManager {
Context ctx;
static String dir;
public CalManager(Context c){
this.ctx = c;
dir = ctx.getFilesDir().toString();
}
/** Application name. */
private static final String APPLICATION_NAME =
"test";
/** 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;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY =
JacksonFactory.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
/** Global instance of the scopes required by this quickstart.
*
* If modifying these scopes, delete your previously saved credentials
* at ~/.credentials/calendar-java-quickstart
*/
private static final List<String> SCOPES =
Arrays.asList(CalendarScopes.CALENDAR_READONLY);
static {
try {
HTTP_TRANSPORT = new com.google.api.client.http.javanet.NetHttpTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
t.printStackTrace();
}
}
/**
* Creates an authorized Credential object.
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
CalManager.class.getResourceAsStream("clientsecret.json");
if(in==null){
Log.d("mychecks", "fail");
}
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("user");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Calendar client service.
* @return an authorized Calendar client service
* @throws IOException
*/
public static com.google.api.services.calendar.Calendar
getCalendarService() throws IOException {
Credential credential = authorize();
return new com.google.api.services.calendar.Calendar.Builder(
HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
}
public void addEvent() throws IOException{
com.google.api.services.calendar.Calendar service =
getCalendarService();
Event event = new Event()
.setSummary("TestCalendarEvent")
.setLocation("800 Howard St., San Francisco, CA 94103")
.setDescription("First Test");
DateTime startDateTime = new DateTime("2017-11-07T22:00:00Z");
EventDateTime start = new EventDateTime()
.setDateTime(startDateTime);
event.setStart(start);
DateTime endDateTime = new DateTime("2017-11-07T23:00:00Z");
EventDateTime end = new EventDateTime()
.setDateTime(endDateTime);
event.setEnd(end);
EventReminder[] reminderOverrides = new EventReminder[] {
new EventReminder().setMethod("popup").setMinutes(10),
};
Event.Reminders reminders = new Event.Reminders()
.setUseDefault(false)
.setOverrides(Arrays.asList(reminderOverrides));
event.setReminders(reminders);
String calendarId = "bagcf5aip1o2v7ek6inb5693m8@group.calendar.google.com";
event = service.events().insert(calendarId, event).execute();
}
}
此时,应用程序在到达目标位置时崩溃
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
-由于
CalManager.class.getResourceAsStream("clientsecret.json");
-返回null的语句。总是
clientsecret.json-file位于以下位置:
关于如何解决这个问题有什么想法吗
这个代码还有其他明显的问题吗
谢谢你的帮助
编辑:
getResourceAsStream()-问题已解决
下一个问题是
private static final java.io.File DATA_STORE_DIR = new java.io.File(
dir , ".credentials/test");
引发错误:java.io.IOException:无法创建目录:/。凭据/测试
有什么想法吗?把你的
json
文件放在资产文件夹中
并获得如下输入流:
<?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"/>
<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>
InputStream is = context.getAssets().open("clientsecret.json");
将您的
json
文件放在资产文件夹中
并获得如下输入流:
<?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"/>
<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>
InputStream is = context.getAssets().open("clientsecret.json");
添加了另一个问题的编辑。对此有什么想法吗?:/谢谢你的帮助!添加了另一个问题的编辑。对此有什么想法吗?:/谢谢你的帮助!