【问题标题】:Android WebView not allowed to load local video fileAndroid WebView 不允许加载本地视频文件
【发布时间】:2015-03-08 17:30:28
【问题描述】:

我有一个带有显示 HTML 文件的 WebView 的应用程序。在 HTML 文件中,有一个按钮会要求用户录制视频,或者从他的文档文件夹中选择一个视频。

在选择(或录制)视频时,它会调用一个带有指向视频文件的链接(由 Uri 编码)的 javascript 函数,然后它应该通过将其设置为源来将其显示在一个元素中:

function showPreview(previewFile){
    console.log(previewFile);
    document.getElementById('previewVideo').src = previewFile;
}

我遇到了这个错误,我一直在四处寻找,但似乎找不到解决方案:

I/chromium﹕ [INFO:CONSOLE(94)] "content://com.android.providers.media.documents/document/video%3A19961", source: file:///android_asset/index.html (94)
W/MediaResourceGetter﹕ permission denied to access network state
W/MediaResourceGetter﹕ non-file URI can't be read due to unsuitable network conditions
E/MediaResourceGetter﹕ Unable to configure metadata extractor

如您所见,我在我的 javascript 函数中记录了指向视频文件的链接,您可以将其告知指向 content://com.android.providers.media.documents/document/video%3A19961 的链接。

这就是我在代码中加载 WebView 的方式(当然,在 XML 中也有相应的 WebView):

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    webView = (WebView) this.findViewById(R.id.webView);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowFileAccessFromFileURLs(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    webView.addJavascriptInterface(new CSJSInterface(getApplicationContext()), "jsInterface");
    webView.loadUrl("file:///android_asset/index.html");
}

Javascript接口函数&回调

 @JavascriptInterface
 public void showCapture() {
     File imageStorageDir = new File(
             Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
             , CS_MOVIE_DIRECTORY);

     // Create the directory if needed:
     if (!imageStorageDir.exists()) {
         imageStorageDir.mkdirs();
     }

     // Create camera captured image file path and name
     File file = new File(
             imageStorageDir + File.separator + "MOV_"
                     + String.valueOf(System.currentTimeMillis())
                     + ".mp4");
     mCapturedImageURI = Uri.fromFile(file);
     // Camera capture image intent
     final Intent captureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
     captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
     Intent i = new Intent(Intent.ACTION_GET_CONTENT);
     i.addCategory(Intent.CATEGORY_OPENABLE);
     i.setType("video/*");

     // Create file chooser intent
     Intent chooserIntent = Intent.createChooser(i, "Video Chooser");

     // Set camera intent to file chooser
     chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Parcelable[]{captureIntent});

     // On select image call onActivityResult method of activity
     startActivityForResult(chooserIntent, CAMERA_CAPTURE_RESULT);
 }

调用 javascript 以链接选择/录制的视频文件:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    LogUtils.log(LogUtils.DEBUG, "onActivityResult called: " + requestCode + " ," + resultCode);

    if (requestCode == CAMERA_CAPTURE_RESULT) {
        // Test if the WebView is loaded:
        if (webView != null) {
            LogUtils.log(LogUtils.DEBUG, "Calling javascript to set preview video.");
            webView.loadUrl("javascript: showPreview('" + Uri.encode(data.getData().toString()) + "');");
        }
    }
}

AndroidManifest.xml

这是我的清单,因为我假设权限可能会起作用

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tomspee.comingsoon" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permissions.READ_EXTERNAL_STORAGE" />

<application
    android:hardwareAccelerated="true"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

【问题讨论】:

    标签: javascript android video webview android-contentprovider


    【解决方案1】:

    意识到这是旧的,在我自己的问题中解决它。我想我会应用一些答案。

    首先,READ_EXTERNAL_STORAGE 是“权限”,而不是“权限”。

    其次,webview 显然也需要清单中的 ACCESS_NETWORK_STATE 权限,因为某些类型的媒体播放流系统会查看网络状态并尝试为流预取元数据。添加,MediaResourceGetter 错误将消失。

    另外,与我的问题无关,我已经看到某些 URL 结构可能适用于 webview 本身,但不适用于其他子系统。不确定 content:// 是否是其中之一...

    希望对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 1970-01-01
      • 2021-09-01
      • 1970-01-01
      • 2019-03-23
      • 2018-03-16
      • 2016-01-25
      相关资源
      最近更新 更多