13

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

curtiss
  • 181
  • 1
  • 2
  • 9
  • Which android version you are using for the test? – Prokash Sarkar Jul 29 '17 at 05:06
  • question is matching with [https://stackoverflow.com/questions/8885701/play-local-hard-drive-video-file-with-html5-video-tag](https://stackoverflow.com/questions/8885701/play-local-hard-drive-video-file-with-html5-video-tag) – PCGALI ANDROID Jul 31 '17 at 12:01
  • 2
    try this for your solution [https://stackoverflow.com/questions/14194890/playing-local-video-in-webview-on-android](https://stackoverflow.com/questions/14194890/playing-local-video-in-webview-on-android) – PCGALI ANDROID Jul 31 '17 at 12:07
  • 1
    When you say "Please provide a complete solution to play a local video stored in sdcard using webview which open url MyDomain.com\video.html" are you asking for someone to take the one method you have written and develop the rest of the application for you? What have you tried? I suggest that the user who wrote the bounty notice to attempt to do this and post any errors they have as a question – cjnash Aug 03 '17 at 14:40

2 Answers2

3

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

Odufuwa Segun
  • 274
  • 1
  • 9
  • Asker requested in Bounty notice : _`"Please provide a complete solution to play a local video stored in sdcard using webview which usually opens url like MyDomain.com\video.htm"`_. – VC.One Aug 03 '17 at 03:03
3

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

Amit Sharma
  • 645
  • 5
  • 13
  • Hi, this is at risk of being a **low-quality answer**. One sentence is not enough for requested _`"Please provide a complete solution to play a local video stored in sdcard using webview which usually opens url like MyDomain.com\video.htm"`_. Your first link was mentioned in comments 2 days ago, also second link is from 2013 and there is comment warning it doesn't work in Android 4.4 and above (2014). Please [**edit**](https://stackoverflow.com/posts/45469216/edit) with explained solution and providing tested example code. – VC.One Aug 03 '17 at 03:01