Android WebView不允许加载本地视频文件 [英] Android WebView not allowed to load local video file

查看:1178
本文介绍了Android WebView不允许加载本地视频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有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:拒绝访问网络状态的权限
W / MediaResourceGetter:由于网络条件不合适,无法读取非文件URI
E / MediaResourceGetter:无法配置元数据提取器

正如您所看到的,我正在我的javascript函数中记录视频文件的链接,您可以告诉我们链接到内容://com.android.providers .media.documents / document / video%3A19961



这是我在我的代码中加载WebView的方式(并且有一个相应的WebView in当然是XML):

  @Override 
protected void onCreate(Bundle savedInstanceS tate){
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);

//如果需要,创建目录:
if(!imageStorageDir.exists()){
imageStorageDir.mkdirs();
}

//创建相机捕获的图像文件路径和名称
文件文件=新文件(
imageStorageDir + File.separator +MOV_
+ String.valueOf(System.currentTimeMillis())
+。mp4);
mCapturedImageURI = Uri.fromFile(file);
//摄像头捕获图像意图
final Intent captureIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT,mCapturedImageURI);
意图i =新意图(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType(video / *);

//创建文件选择器意图
Intent chooserIntent = Intent.createChooser(i,Video Chooser);

//将相机意图设置为文件选择器
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,new Parcelable [] {captureIntent});

//在选择图像上调用onActivityResult活动方法
startActivityForResult(chooserIntent,CAMERA_CAPTURE_RESULT);
}

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

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

if(requestCode == CAMERA_CAPTURE_RESULT){
//测试是否加载了WebView:
if(webView!= null){
LogUtils.log(LogUtils) .DEBUG,调用javascript来设置预览视频。);
webView.loadUrl(javascript:showPreview('+ Uri.encode(data.getData()。toString())+'););
}
}
}



AndroidManifest.xml



这是我的清单,因为我假设权限可能正在发挥作用

 <?xml version =1.0encoding =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>

解决方案

意识到这已经很老了,在我自己的问题上打了它。我以为我会申请一些答案。



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



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



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



希望能帮到某人。


I have an application with a WebView showing an HTML file. In the HTML file, there's a button that will request the user to record video, or select a video from his documents folder.

Upon selecting (or recording) a video, it calls a javascript function with the link (encoded by Uri) to the video file, which it should then display in a element, by setting it as its source:

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

I'm running into this error and I've been looking around but can't seem to find the solution:

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

As you can see I'm logging the link to the video file in my javascript function, which as you can tell links to content://com.android.providers.media.documents/document/video%3A19961.

This is how I load the WebView in my code (and there's a corresponding WebView in the XML of course):

@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 Interface function & callback

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

Call to javascript to link the video file that was selected/recorded:

@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

Here's my manifest, as I'm assuming the permissions are likely playing a role

<?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>

解决方案

Realize this is old, hit it in my own problem. Thought I'd apply some answers.

first, READ_EXTERNAL_STORAGE is "permission.", not "permissions."

Second, webview apparently needs ACCESS_NETWORK_STATE permission in the manifest as well, as certain types of media playback the streaming system looks at the network status and tries to prefetch metadata for the stream. Add that, MediaResourceGetter errors will go away.

Separately, not related to my problem, I have seen that certain URL structures might work for say webview itself, but not other subsystems. Not sure if content:// is one of those...

hope that helps someone.

这篇关于Android WebView不允许加载本地视频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆