Android: Unable to play video from private path su

2019-08-10 00:37发布

问题:

I am able to play video from private path /data/data/com.exmaple.ui/files/final.mp4 this path.

But unable to play from subdirectory like /data/data/com.exmaple.ui/files/myVideos/final.mp4,

Intent intent = new Intent(Intent.ACTION_VIEW);
File playFile = new File("/data/data/com.exmaple.ui/files/myVideos/final.mp4");
intent.setDataAndType(Uri.fromFile(playFile), "video/mp4");
startActivity(intent);

File Creation code:

    String path =  getFilesDir().getAbsolutePath();
    File dest = new File(path,"myVideos");
    boolean mkdirs = dest.mkdirs();
    File destFinal =  new File(dest,"final.mp4");
    destFinal.setReadable(true, false);
    copyFileUsingFileStreams(inputfile,destFinal);

Copy Code:

private void copyFileUsingFileStreams(File source, File dest) throws IOException {

    InputStream input = null;
    OutputStream output = null;

    try {
        input = new FileInputStream(source);
        output = new FileOutputStream(dest);
        byte[] buf = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buf)) > 0) {
            output.write(buf, 0, bytesRead);
        }
        dest.setReadable(true);
    } finally {
        input.close();
        output.close();
    }

Have made file readable,its not allowing me to read the file like above using intents any reason?

Error : Device 1:

07-09 14:22:12.098: W/VideoView(17106): Unable to open content: file:///data/data/com.exmaple.ui/files/myVideos/final.mp4
07-09 14:22:12.098: W/VideoView(17106): java.io.IOException: setDataSource failed.

Error :Device 2:

----------Private File canRead :true Exists :true
    07-09 20:06:00.636: W/System.err(19371): java.io.FileNotFoundException: /sys/class/tcon/tcon/mode: open failed: ENOENT (No such file or directory)
    07-09 20:06:00.636: W/System.err(19371):    at libcore.io.IoBridge.open(IoBridge.java:409)
    07-09 20:06:00.636: W/System.err(19371):    at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
    07-09 20:06:00.636: W/System.err(19371):    at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
    07-09 20:06:00.636: W/System.err(19371):    at com.sec.android.hardware.SecHardwareInterface.sysfsWrite(SecHardwareInterface.java:100)
    07-09 20:06:00.636: W/System.err(19371):    at com.sec.android.hardware.SecHardwareInterface.setTconUIMode(SecHardwareInterface.java:343)
    07-09 20:06:00.636: W/System.err(19371):    at com.sec.android.app.videoplayer.activity.MoviePlayer$SecHWInterfaceWrapper.setTconUIMode(MoviePlayer.java:5980)
    07-09 20:06:00.636: W/System.err(19371):    at com.sec.android.app.videoplayer.activity.MoviePlayer$24.handleMessage(MoviePlayer.java:3644)
    07-09 20:06:00.636: W/System.err(19371):    at android.os.Handler.dispatchMessage(Handler.java:99)
    07-09 20:06:00.636: W/System.err(19371):    at android.os.Looper.loop(Looper.java:137)
    07-09 20:06:00.636: W/System.err(19371):    at android.app.ActivityThread.main(ActivityThread.java:5455)
    07-09 20:06:00.636: W/System.err(19371):    at java.lang.reflect.Method.invokeNative(Native Method)
    07-09 20:06:00.646: W/System.err(19371):    at java.lang.reflect.Method.invoke(Method.java:525)
    07-09 20:06:00.646: W/System.err(19371):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
    07-09 20:06:00.646: W/System.err(19371):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    07-09 20:06:00.646: W/System.err(19371):    at dalvik.system.NativeStart.main(Native Method)
    07-09 20:06:00.646: W/System.err(19371): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
    07-09 20:06:00.646: W/System.err(19371):    at libcore.io.Posix.open(Native Method)
    07-09 20:06:00.646: W/System.err(19371):    at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
    07-09 20:06:00.646: W/System.err(19371):    at libcore.io.IoBridge.open(IoBridge.java:393)
    07-09 20:06:00.646: W/System.err(19371):    ... 14 more

Wonder how root and subfloder making a difference here? any restrictions mentioned? File provider or the Content provider are the options? Thanks Nitz

回答1:

Since you have this exception:

java.io.FileNotFoundException: /sys/class/tcon/tcon/mode: open failed: ENOENT (No such file or directory)

Means that the file doesn´t exist or you don´t have the correct path to the file.

Use instead:

    File playFile = new File(Environment.getExternalStorageDirectory() + "/data/data/com.exmaple.ui/files/myVideos/final.mp4");
    if(playFile.exists()){ 
        intent.setDataAndType(Uri.fromFile(playFile), "video/mp4");
        startActivity(intent);
    }else{
        Toast.makeText(getApplicationContext(), "The file doesn´t exist!", Toast.LENGTH_LONG).show();
    }

or

    File playFile = new File(Environment.getExternalStorageDirectory() + "/data/data/com.exmaple.ui/files/myVideos", "final.mp4");
    if(playFile.exists()){ 
        intent.setDataAndType(Uri.fromFile(playFile), "video/mp4");
        startActivity(intent);
    }else{
        Toast.makeText(getApplicationContext(), "The file doesn´t exist!", Toast.LENGTH_LONG).show();
    }

And very important remember to add the permission:

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>