Android WebView不允许加载本地视频文件

时间:2015-01-10 13:30:31

标签: javascript android video webview android-contentprovider

我有一个带有WebView的应用程序,显示一个HTML文件。在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>

1 个答案:

答案 0 :(得分:4)

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

首先,READ_EXTERNAL_STORAGE是&#34;权限。&#34;,不是&#34;权限。&#34;

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

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

希望能有所帮助。

相关问题