能否videoview播放存储在内部存储的视频? [英] Can a videoview play a video stored on internal storage?

查看:824
本文介绍了能否videoview播放存储在内部存储的视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的用户使用内部或外部存储的能力。我同时显示图像和视频(一个科学性的)。当SD卡上存储的媒体,所有的罚款。但是,当我在内部存储介质,只有图像显示。不管我怎么努力,我得到的各种错误试图加载和显示下applicationcontext.getFilesDir()。

存储介质时

有一招一videoview的内容设置为这样的文件?

能否ContentResolver的帮助我吗?

在一个相关的说明,是它认为不好的形式假设外部存储的存在?

在此先感谢,

希德

下面是失败,无法播放视频。很抱歉,此视频无法播放的一个版本。但我有很多失败的其他模式。我可以复制的内部视频到临时存储(外部)和播放,所以这个副本内部确实创建一个有效的电影。它只有当我尝试直接从内部存储播放失败。

  videoFile =新的文件(this.getFilesDir()+文件分割符+test.mp4);


InputStream的数据= res.openRawResource(R.raw.movi​​egood);


尝试 {
    的OutputStream myOutputStream =新的FileOutputStream(videoFile);


    byte []的缓冲区=新的字节[8192];
    INT长;
    而((长度= data.read(缓冲液))大于0){
        myOutputStream.write(缓冲液);
    }

    //关闭流
    myOutputStream.flush();
    myOutputStream.close();
    data.close();
}赶上(FileNotFoundException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}赶上(IOException异常E){
    // TODO自动生成的catch块
    e.printStackTrace();
}




vview.setKeepScreenOn(真正的);
vview.setVideoPath(videoFile.getAbsolutePath());
vview.start();
 

解决方案

MediaPlayer的要求,正在播放的文件具有世界可读权限。您可以查看文件的权限,在亚洲开发银行的外壳下面的命令:

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

您可能会看到-rw ------,这意味着,只有所有者(您的应用程序,而不是MediaPlayer的)具有读/写权限。

请注意:您的手机必须植根才能使用ls命令,而无需指定文件(在内存中)

如果您的手机扎根,你可以在亚行外壳加上世界读取权限使用以下命令:

 文件模式Ø+ R /data/data/com.mypackage/myfile
 

如果您需要以编程方式修改这些权限(!需要根植电话),您可以在您的应用程序code。使用以下命令:

 调用Runtime.getRuntime()EXEC(文件模式Ø+ R /data/data/com.mypackage/myfile);
 

这基本上是一个linux命令。请参阅<一href="https://help.ubuntu.com/community/FilePermissions">https://help.ubuntu.com/community/FilePermissions更多有关文件模式。

编辑:找到另一种简单的方法<一href="http://groups.google.com/group/android-developers/browse_thread/thread/5569a88c50dcc043/31d7da67e79689d7?lnk=raot&fwc=1">here (适用于那些没有根植电话)。由于应用程序拥有这个文件,它可以创建一个文件描述符,并传递到mediaPlayer.setDataSource():

 的FileInputStream的FileInputStream =新的FileInputStream(/数据/数据​​/ com.mypackage / MYFILE);
mediaPlayer.setDataSource(fileInputStream.getFD());
 

此方法完全避免了权限问题。

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 the media on the SD card, all is fine. But when I store the media internally, only the images will display. No matter what I try I get various errors when trying to load and display the media stored under the applicationcontext.getFilesDir().

Is there a trick to setting a videoview's content to such a file?

Can a ContentResolver help me?

On a related note, is it considered bad form to assume that external storage exists?

Thanks in advance,

Sid

Below is one version that fails with "Cannot play video. Sorry, this video cannot be played". But I have many other modes of failure. I can copy the internal video to temp storage (external) and play it, so this copy to internal does indeed create a valid movie. It only fails when I try to play it directly from the internal storage.

videoFile = new File(this.getFilesDir() + File.separator + "test.mp4");


InputStream data = res.openRawResource(R.raw.moviegood);


try {
    OutputStream myOutputStream = new FileOutputStream(videoFile);


    byte[] buffer = new byte[8192];
    int length;
    while ( (length = data.read(buffer)) > 0 ) {
        myOutputStream.write(buffer);
    }

    //Close the streams
    myOutputStream.flush();
    myOutputStream.close();
    data.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}




vview.setKeepScreenOn(true);
vview.setVideoPath(videoFile.getAbsolutePath());
vview.start();

解决方案

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.

这篇关于能否videoview播放存储在内部存储的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆