twicca plug-inのつくりかた

しかだよ。
twicca shutter plug-in作ったので技術公開します。主にメディアアップロードプラグインの作り方になります。

公式ドキュメント

http://twicca.r246.jp/plugins/documents/
androidやってる方は読めば分かるかも知れないけど、しかは未熟なのでわからないことが多かった。

おおまかな作り方

twiccaからのメディアの受け取り方

getIntent()でもらえます。

InputStream openInputStream = null;
try{
    Intent receivedIntent = getIntent();
    Uri uri = receivedIntent.getData();
    openInputStream = getContentResolver().openInputStream(uri);
}...
アップロードするメディアの種類を決める

mimeTypeをどこに書くのかわかんなかった。Manifestファイルのactivityのintent-filterに書くみたいです。画像や動画の様に複数種類ある場合は、複数行に分けて書いてください。

        <activity android:name=".MainActivity"
         android:label="@string/acitivity_name"
         android:theme="@android:style/Theme.Translucent">
            <intent-filter>
               <action android:name="jp.r246.twicca.ACTION_UPLOAD" />
               <category android:name="android.intent.category.DEFAULT" />
               <data android:mimeType="image/*" />
               <data android:mimeType="video/*" />
           </intent-filter>
        </activity>
変換画面は透明にしたい

twiccaの画面を隠さず処理したい場合、activityを透明にします。activityタグのthemeにTranslucentの設定してください。applicationタグに書くと、他のactivityも透明になるので注意。

        <activity android:name=".MainActivity"
         android:label="@string/acitivity_name"
         android:theme="@android:style/Theme.Translucent">
メディアと文字列を同時にPostできない

MultipartEntityを使ってください。
http://hc.apache.org/downloads.cgi
HttpClient 4.1の中に入っている httpmime-4.1.jar をAndroidプロジェクトの中に突っ込んで、Pathを通してください。(僕はlibディレクトリを作ってpathを通しました。)

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(UPLOAD_URL);
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("key", new StringBody(key, Charset.forName("UTF-8")));
// TODO メッセージに何を送る?
entity.addPart("message", new StringBody("shutter plugin sample message", Charset.forName("UTF-8")));
String type = getIntent().getType();
// TODO ファイル名をどうする?
InputStreamBody streamBody = new InputStreamBody(inputStream, type,"shutter_plugin_sample.jpg");
entity.addPart("media", streamBody);
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);

これで写真や動画のメディアとファイル名などの文字列を同時にサーバーにPostできます。

plug-inで使うのはActivity

どうやらtwiccaから呼ばれるのはActivityじゃないとだめっぽいです。レシーバやサービスでやってみましたがだめでした。