Can a videoview play a video stored on internal storage?

前端 未结 6 1490
一向
一向 2020-11-29 04:12

I\'m trying to provide my users with the ability to use either external or internal storage. I\'m displaying both images and videos (of a scientific nature). When storing th

6条回答
  •  Happy的楠姐
    2020-11-29 04:33

    MediaPlayer requires that the file being played has world-readable permissions. You can view the permissions of the file with the following command in adb shell:

    ls -al /data/data/com.mypackage/myfile
    

    You will probably see "-rw------", which means that only the owner (your app, not MediaPlayer) has read/write permissions.

    Note: Your phone must be rooted in order to use the ls command without specifying the file (in the internal memory).

    If your phone is rooted, you can add world-read permissions in adb shell with the following command:

    chmod o+r /data/data/com.mypackage/myfile
    

    If you need to modify these permissions programmatically (requires rooted phone!), you can use the following command in your app code:

    Runtime.getRuntime().exec("chmod o+r /data/data/com.mypackage/myfile");
    

    Which is basically a linux command. See https://help.ubuntu.com/community/FilePermissions for more on chmod.

    EDIT: Found another simple approach here (useful for those without rooted phones). Since the application owns the file, it can create a file descriptor and pass that to mediaPlayer.setDataSource():

    FileInputStream fileInputStream = new FileInputStream("/data/data/com.mypackage/myfile");
    mediaPlayer.setDataSource(fileInputStream.getFD());
    

    This approach avoids the permission issue completely.

提交回复
热议问题