【问题标题】:Android: Unable to play video from private path subdirectory using intentAndroid:无法使用意图从私有路径子目录播放视频
【发布时间】:2015-07-11 02:39:43
【问题描述】:

我可以从私有路径 /data/data/com.exmaple.ui/files/final.mp4 这个路径播放视频。

但无法从 子目录(如 /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);

文件创建代码:

    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);

复制代码:

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();
    }

已经使文件可读,它不允许我像上面那样使用意图读取文件吗?

错误: 设备 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.

错误:设备 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

想知道 root 和 subfloder 在这里有何不同吗?提到任何限制? 文件提供者或内容提供者是选项? 谢谢 尼兹

【问题讨论】:

  • 为什么有internal,你可以复制到你的SD或者更改权限。
  • 要求是从内存中存储和播放
  • requirement 很可能来自不了解 Android 工作原理的人。
  • @Jared Burrows 认真的吗?忘记要求......让我知道一般......
  • 我已经回答了你的问题。阅读我的第一条评论。或者你可以试试FileProvider:stackoverflow.com/questions/21304489/…

标签: android file android-intent memory


【解决方案1】:

既然你有这个例外:

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

表示该文件不存在或您没有该文件的正确路径。

改用:

    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();
    }

    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();
    }

记住添加权限非常重要:

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

【讨论】:

  • ---------Private File canRead :true Exists :true...我的文件存在..请查看日志..我的要求是从内部存储器播放..
  • /data/data/com.exmaple.ui 是内部存储,而不是外部存储。该操作需要先在 SD 卡上创建它。
  • @JaredBurrows “操作需要先在 SD 卡上创建它”是什么意思?要求是从内部存储器存储和播放。
  • @NitZRobotKoder 你刚刚回答了你自己的问题。这篇文章是针对 SD 卡的。
  • @JaredBurrows 检查问题..我正在进行可行性测试..问题和答案无关紧要..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-07
  • 2010-10-09
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多