2017-11-09 3 views
0

私はしばらく検索していましたが、答えは試しましたが、これまでのところ何もできませんでした。 ボタンを押すことでプログラムでGoogleカレンダーイベントを作成したいと考えています。class.getResourceAsStream()はヌルを返します

<?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> 

なく、少なくとも最後に:

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 = "[email protected]"; 
     event = service.events().insert(calendarId, event).execute(); 
    } 
} 

マイたManifest.xmlは次のようになります。次のようにCalManagerクラスが見え

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

: は私のMainActivityのために、次のコードを手に入れました、私は次のgradleファイルを持っています:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.2" 
    configurations.all { 
     resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9' 
    } 
    defaultConfig { 
     applicationId "com.example.user.test" 
     minSdkVersion 21 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner 
"android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 
'proguard-rules.pro' 
     } 
    } 
} 

repositories { 
    mavenCentral() 
} 

dependencies { 
    compile fileTree(include: ['*.jar'], dir: 'libs') 
    androidTestCompile('com.android.support.test.espresso:espresso- 
    core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.1.0' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    testCompile 'junit:junit:4.12' 
    compile 'com.google.api-client:google-api-client:1.23.0' 
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0' 
    compile 'com.google.apis:google-api-services-calendar:v3-rev264-1.23.0' 
} 
この時点で210

、アプリがクラッシュCalManagerのAUTHORIZE()に

GoogleClientSecrets clientSecrets = 
       GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

-statementに達した - ので、nullを返す

CalManager.class.getResourceAsStream("clientsecret.json"); 

-statementの方法。常に。

clientsecret.jsonファイルは次の場所にあります。この問題を解決する方法について enter image description here

任意のアイデア?

さらに、このコードには他に明らかな問題がありますか?

ありがとうございました!

編集: getResourceAsStream() - 問題が解決しました。 java.io.IOException:ディレクトリを作成できません:/.credentials/test

1つの任意のアイデアを?

次の問題が

private static final java.io.File DATA_STORE_DIR = new java.io.File(
     dir , ".credentials/test"); 

がエラーをスローしていること、ですか

答えて

2

資産フォルダ

enter image description here

であなたのjsonファイルを入れて、このような入力ストリームを取得:

InputStream is = context.getAssets().open("clientsecret.json"); 
+0

を作品、感謝:) – Zimbaa

+0

は、さらに約issue..anyアイデアを編集を追加しましたこれです? :/ 助けてくれてありがとう! – Zimbaa

関連する問題