I want to play local videos in crosswalk webview. I'm getting the video path from an intent opening my activity, so it could be stored internally or on sd card.
Using this url file:///storage/emulated/0/Download/movies/movie.mp4
i get Failed to load resource: net::ERR_ACCESS_DENIED
.
Using input file dialog with URL.createObjectURL(file);
it works. But because i know the path, i dont want to select it again manually. Setting the value of input file is forbidden.
Creating a url using the path is also not possible, because it needs file or blob.
Because of possible big filesize its not possible to temporarily copy it to sd card.
This gives me file not found error:
@Override public WebResourceResponse shouldInterceptLoadRequest(XWalkView view, String url) { if(url.contains("file:///storage")){ try { return new WebResourceResponse("video/*", "UTF-8", new FileInputStream(url)); } catch (FileNotFoundException e) { e.printStackTrace(); } } return super.shouldInterceptLoadRequest(view, url); }
also i have defined <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
other questions on stackoverflow asking for accessing assets, so i created this new question
2 Answers
Answers 1
To access local files on android devices running marshmellow or above, you are required to check the permission at runtime before trying to access the file. If you don't, it will break
Here is a tutorial where it is implemented, specifically reading of files https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/
Depending on the source of the file, I would recommend you not checking before playing but checking just before the entry point. For example, if you are trying to select a media from gallary, or a file from the file manager, just before you create the file intent picker, you check for the write permission, you then proceed as shown below
@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == EXTERNAL_STORAGE_PERMISSION_CONSTANT) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //The External Storage Write Permission is granted to you... Continue your left job... proceedAfterPermission(); } else { //request again for permission or assume permission denied and alert the user } } }
You should still study the link, its pretty easy to implement. Hopefully you'll find this helpful
Answers 2
The problem might be in accessing video file from External storage also make sure you have given read/write Run time permission . Here you can find your solution here and this
0 comments:
Post a Comment