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

标签 android cordova android-webview android-camera

我是 Android 新手。

我按照 tutorial 在我的 Android 应用程序中嵌入了 Cordova WebView .

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

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

教程建议我需要实现 CordovaInterface 来使用相机,如下所示。

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

我不知道activityResultCallback到底是什么。是否有其他教程向我展示如何实现此接口(interface)?

最佳答案

因为没有人回答我的问题。

我找到一个 tutorial可以解决这个问题。

更新: 鉴于链接已损坏,我将发布我自己的用于实现 Cordova 接口(interface)的代码。

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

关于android - 如何实现 CordovaInterface 以便我可以在 webview 中使用相机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14282159/

相关文章:

android - 在中间修剪 TextView,保留结尾字符

android - 已解决 : ListView not getting scrolled smoothly and it stucks/crashes while scrolling even with ViewHolder

xcode - 找不到 Cordova/CDVViewController.h 文件

javascript - 如何设计一个 Web 应用程序以允许多个存储提供商?

android - 需要将 HTML 页面(使用 CSS)显示为翻页的书

android - 创建点击 WebView 链接的 Intent

android - 是否可以使用 pdf.js 在 Android WebView 中显示 pdf?

android - 扩展 ActionBarActivity 导致错误

Android RelativeLayout 将一个 View 的中心对齐到另一个 View 的右上角

javascript - 使用javascript使用phonegap调用电话短信api