首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Cordova Android单击input type=file会打开系统文件浏览器,而不是相机选项。

Cordova Android单击input type=file会打开系统文件浏览器,而不是相机选项。
EN

Stack Overflow用户
提问于 2019-08-21 07:08:58
回答 1查看 2.1K关注 0票数 1

问题

在Cordova Android中,当单击html type=" file“输入时,系统文件选择器将出现,而不是相机选项。

预计会发生什么?

如果我在系统浏览器中执行相同的操作,就会得到预期的对话框:

版本信息

代码语言:javascript
运行
复制
"cordova-android": "^8.0.0",
"cordova-plugin-camera": "^4.1.0"

科多瓦-克莱9.0.0

测试用例回购

这是我的第一个Android Cordova应用程序,我是不是缺少了某个插件?我不记得被要求在任何阶段使用相机的许可,所以我想知道我是不是错过了上面链接的config.xml中的什么东西?

EN

回答 1

Stack Overflow用户

发布于 2019-08-21 22:05:17

您需要设置captureaccept属性,如下所示:

代码语言:javascript
运行
复制
<input type="file" capture="camera" accept="image/*" />

编辑

另外,确保您的platforms/android/AndroidManifest.xml包含以下元素:

代码语言:javascript
运行
复制
<application android:allowBackup="false" android:hardwareAccelerated="true" android:icon="@mipmap/icon" android:label="@string/app_name">
    <provider android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true" android:name="android.support.v4.content.FileProvider">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" />
    </provider>
    <provider android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true" android:name="org.apache.cordova.camera.FileProvider">
        <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/camera_provider_paths" />
    </provider>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如果它们不存在,则创建包含以下内容的文件platforms/android/res/xml/file_paths.xml

代码语言:javascript
运行
复制
<!-- file_paths.xml -->
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="photos" path="photos/" />
</paths>

和文件platforms/android/res/xml/camera_provider_paths.xml

代码语言:javascript
运行
复制
<!-- camera_provider_paths.xml -->
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

如果这似乎仍然不起作用,则可能还需要在platforms/android/CordovaLib/src/org/apache/cordova/engine目录中添加/修改platforms/android/CordovaLib/src/org/apache/cordova/engine文件,以包括以下内容:

代码语言:javascript
运行
复制
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onShowFileChooser(WebView webView, final ValueCallback<Uri[]> filePathsCallback, final WebChromeClient.FileChooserParams fileChooserParams) {
    // Image from file intent
    boolean multiple = fileChooserParams.getMode() == WebChromeClient.FileChooserParams.MODE_OPEN_MULTIPLE;
    String type = "*/*";
    if (fileChooserParams.getAcceptTypes() != null && fileChooserParams.getAcceptTypes().length > 0) {
        type = fileChooserParams.getAcceptTypes()[0];
    }
    Intent fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    fileIntent.addCategory(Intent.CATEGORY_OPENABLE);
    fileIntent.setTypeAndNormalize(type);
    fileIntent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, multiple);

    // Image from camera intent
    Uri tempUri = null;
    Intent captureIntent = null;
    if (fileChooserParams.isCaptureEnabled()) {
        captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Context context = parentEngine.getView().getContext();
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA) && captureIntent.resolveActivity(context.getPackageManager()) != null) {
            try {
                File tempFile = createTempFile(context);
                Log.d(LOG_TAG, "Temporary photo capture file: " + tempFile);
                tempUri = createUriForFile(context, tempFile);
                Log.d(LOG_TAG, "Temporary photo capture URI: " + tempUri);
                captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
            } catch (IOException e) {
                Log.e(LOG_TAG, "Unable to create temporary file for photo capture", e);
                captureIntent = null;
            }
        } else {
            Log.w(LOG_TAG, "Device does not support photo capture");
            captureIntent = null;
        }
    }
    final Uri captureUri = tempUri;

    // Chooser intent
    Intent chooserIntent = Intent.createChooser(fileIntent, null);
    if (captureIntent != null) {
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent });
    }

    try {
        Log.i(LOG_TAG, "Starting intent for file chooser");
        parentEngine.cordova.startActivityForResult(new CordovaPlugin() {
            @Override
            public void onActivityResult(int requestCode, int resultCode, Intent intent) {
                // Handle result
                Uri[] result = null;
                if (resultCode == Activity.RESULT_OK) {
                    List<Uri> uris = new ArrayList<Uri>();
                    if (intent == null && captureUri != null) { // camera
                        Log.v(LOG_TAG, "Adding camera capture: " + captureUri);
                        uris.add(captureUri);

                    } else if (intent.getClipData() != null) { // multiple files
                        ClipData clipData = intent.getClipData();
                        int count = clipData.getItemCount();
                        for (int i = 0; i < count; i++) {
                            Uri uri = clipData.getItemAt(i).getUri();
                            Log.v(LOG_TAG, "Adding file (multiple): " + uri);
                            if (uri != null) {
                                uris.add(uri);
                            }
                        }

                    } else if (intent.getData() != null) { // single file
                        Log.v(LOG_TAG, "Adding file (single): " + intent.getData());
                        uris.add(intent.getData());
                    }

                    if (!uris.isEmpty()) {
                        Log.d(LOG_TAG, "Receive file chooser URL: " + uris.toString());
                        result = uris.toArray(new Uri[uris.size()]);
                    }
                }
                filePathsCallback.onReceiveValue(result);
            }
        }, chooserIntent, FILECHOOSER_RESULTCODE);
    } catch (ActivityNotFoundException e) {
        Log.w("No activity found to handle file chooser intent.", e);
        filePathsCallback.onReceiveValue(null);
    }
    return true;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57586259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档