我有一个带有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函数中记录视频文件的链接,您可以告诉内容链接://com.android.providers.media.documents/document/video:19961.
这就是我在我的代码中加载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);
}
@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中
这是我的清单,因为我假设权限可能正在发挥作用
最佳答案
意识到这是旧的,在我自己的问题中发现它.以为我会应用一些答案.
原文链接:https://www.f2er.com/android/431194.html首先,READ_EXTERNAL_STORAGE是“权限”,而不是“权限”.
其次,webview显然也需要清单中的ACCESS_NETWORK_STATE权限,因为流媒体系统的某些类型的媒体回放会查看网络状态并尝试预取流的元数据.补充一点,MediaResourceGetter错误将消失.
另外,与我的问题无关,我已经看到某些URL结构可能适用于webview本身,但不适用于其他子系统.不确定content://是否是其中之一……
希望有人帮助.