Playing local video in WebView on Android

前端 未结 1 832
夕颜
夕颜 2020-12-20 18:32

I\'m building an app with a WebView thats supposed to play a video, that\'s saved locally. Strangely the video player is not working with local video files. It

相关标签:
1条回答
  • 2020-12-20 19:12

    Slartibartfasts helped me solve this. Since it's just a comment, I'll have to post it myself.

    Essentially it's a permission problem. Here is the link he posted. The suggested method for copying files with MODE_WORLD_READABLE is deprecated, use the method described in the link below and save the files in the external storage

    I had to copy the files to the external storage, and access the video file there. Since the path to the external storage is different within the several versions of android, I copied the all relevant HTML files (including all JS, graphics and video files). See copying files, scroll down for the answer if you have sub folders.

    Here are my copying methods:

    private void copyAssets(String path)
    {
        String[] files = null;
    
        try
        {
            files = getAssets().list(path);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
    
        if (files.length == 0)
            copyFile(path);
        else
        {
            File dir = new File(getExternalFilesDir(null), path);
    
            if (!dir.exists())
                dir.mkdir();
    
            for (int i = 0; i < files.length; i++)
            {
                copyAssets(path + "/" + files[i]);
            }
        }
    }
    
    private void copyFile(String filename)
    {
        InputStream in = null;
    
        File file;
        OutputStream out = null;
    
        try
        {
            in = getAssets().open(filename);
        } catch (IOException e)
        {
            Log.e(TAG, "ERROR WITH in = getAssets().open: " + filename);
            e.printStackTrace();
        }
    
        file = new File(getExternalFilesDir(null), filename);
    
        try
        {
            out = new FileOutputStream(file);
        } catch (FileNotFoundException e)
        {
            Log.e(TAG, "ERROR WITH out = new FileOutputStream(file);");
            e.printStackTrace();
        }
    
        byte[] data;
    
        try
        {
            data = new byte[in.available()];
    
            in.read(data);
            out.write(data);
    
            in.close();
            out.close();
        } catch (IOException e1)
        {
            e1.printStackTrace();
        }
    }
    

    In my onCreate I call

    copyAssets("matrix");
    

    with matrix being the folder in my assets thats holding all files and sub folders.

    0 讨论(0)
提交回复
热议问题