明日から本気出す開発日記

2016/10/31 fc2ブログより引っ越しました。更新はまた明日から本気だす

AS3

【AIR for Android】動画ギャラリーを呼び出すANE

2016/10/31

AIR for AndroidでCameraRollクラスのbrowseForImage()は画像しかギャラリー表示されないので、動画を表示するためのANEを貼ってみる。

ネイティブ側

GalleryExtension

package jp.co.jms.android.VideoGallery.extensions;

import java.util.HashMap;
import java.util.Map;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREExtension;
import com.adobe.fre.FREFunction;

public class GalleryExtension implements FREExtension {

public static FREContext context;

@Override
public FREContext createContext(String arg0) {
if(context == null) {
context = new FREContext() {

@Override
public Map<String, FREFunction> getFunctions() {
Map<String, FREFunction> result = new HashMap<String, FREFunction>();
result.put("VideoGallery", new GalleryStartFunction());
return result;
}

@Override
public void dispose() {}
};
}

return context;
}

@Override
public void dispose() {
// TODO Auto-generated method stub

}

@Override
public void initialize() {
// TODO Auto-generated method stub

}

}

GalleryStartFunction

package jp.co.jms.android.VideoGallery.extensions;

import jp.co.jms.android.VideoGallery.GalleryANENativeActivity;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import com.adobe.fre.FREContext;
import com.adobe.fre.FREFunction;
import com.adobe.fre.FREObject;

public class GalleryStartFunction implements FREFunction {

@Override
public FREObject call(FREContext context, FREObject[] passedArgs) {
try {

Activity activity = context.getActivity();
Intent intent = new Intent(activity, GalleryANENativeActivity.class);
activity.startActivity(intent);

return null;

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

return null;
}

}

GalleryANENativeActivity

package jp.co.jms.android.VideoGallery;

import java.io.File;

import jp.co.jms.android.VideoGallery.extensions.GalleryExtension;
import jp.co.jms.android.VideoGallery.*;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class GalleryANENativeActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// 動画ギャラリー呼び出し
Intent intent = new Intent();

intent.setType("video/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
String str = data.getDataString();

if ( str.indexOf("content:") == 0 ) {
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToPosition( 0 );
String filepath = cursor.getString(1);    // ファイルパス
cursor.close();
GalleryExtension.context.dispatchStatusEventAsync(filepath, "result");
}
super.onActivityResult(requestCode, resultCode, data);
finish();

}
}

ライブラリプロジェクト

package jp.co.jms.android.NativeExtension
{
import flash.events.EventDispatcher;
import flash.events.IEventDispatcher;
import flash.events.StatusEvent;
import flash.external.ExtensionContext;
import flash.text.ReturnKeyLabel;

public class VideoGalleryLibrary extends EventDispatcher
{
private static var extContext: ExtensionContext = null;


public function VideoGalleryLibrary(target:IEventDispatcher=null)
{
extContext = ExtensionContext.createExtensionContext("jp.co.jms.Gallery", "type");
}

public function startVideo(): String
{
extContext.addEventListener(StatusEvent.STATUS, statusHandler);
return extContext.call("VideoGallery") as String;
}

private function statusHandler(event:StatusEvent):void {
dispatchEvent(event);
}

public function dispose(): void
{
return extContext.dispose();
}
}
}

最後に呼び出し元アプリ

import jp.co.jms.nativeExtensions.VideoExtension;

private var extension:VideoExtension;

protected function button1_clickHandler(event:MouseEvent):void
{
extension = new VideoExtension();
extension.addEventListener(StatusEvent.STATUS, statusHandler);
extension.VideoGallery();
}

private function statusHandler(event:StatusEvent):void {
trace(event.code); // 動画のパス
}

-AS3