如何实现CordovaInterface以便我可以在webview中使用相机?

时间:2013-01-11 16:14:39

标签: android cordova android-webview android-camera

我是Android新手。

我按照tutorial

在我的Android应用上嵌入了Cordova WebView

我已使用CordovaWebView成功从我的服务器加载网页。

假设我在该网页上有一个名为“Capture Photo”的按钮,我该如何调用本地API以便我可以使用相机?

教程建议我需要实现CordovaInterface以如下方式使用相机。

@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
    this.activityResultCallback = plugin;        
}

我不知道究竟是什么 activityResultCallback 。是否有另一个教程向我展示如何实现此接口?

1 个答案:

答案 0 :(得分:7)

由于没有人回答我的问题。

我找到了可以解决此问题的tutorial

更新: 鉴于链接已断开,我将发布自己的代码以实现Cordova接口。

// Instance for CordovaInterface
private final ExecutorService threadPool = Executors.newCachedThreadPool();
private boolean mAlternateTitle = false;
private boolean bound;
private boolean volumeupBound;
private boolean volumedownBound;
private CordovaPlugin activityResultCallback;
private Object activityResultKeepRunning;
private Object keepRunning;


public Activity getActivity() {
    return this;
}

@Deprecated
public Context getContext() {
    return this;
}

public ExecutorService getThreadPool() {
    return threadPool;
}


public void setActivityResultCallback(CordovaPlugin plugin) {
    this.activityResultCallback = plugin;

}

public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
    this.activityResultCallback = command;
    this.activityResultKeepRunning = this.keepRunning;

    // If multitasking turned on, then disable it for activities that return
    // results
    if (command != null) {
        this.keepRunning = false;
    }

    // Start activity
    super.startActivityForResult(intent, requestCode);

}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    final CordovaPlugin callback = this.activityResultCallback;
    if (callback != null) {
        // Need to use background thread
        this.getThreadPool().execute(new Runnable() {
            public void run() {
                callback.onActivityResult(requestCode, resultCode, intent);
            }
        });
    }
}