0

看起来我无法播放存储在我的内部应用程序目录中的视频。

我有视频存储在/data/data/my.package.org/files/

我正在尝试使用从那里播放文件

String fpath = "/data/data/my.package.org/files/video.mpg"
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(fpath), "video/*");

但是 Android 默认视频播放器和一些外部视频播放器(MX 播放器)都说“无法播放此视频”。

而当我将视频保存到 SD 卡时,它们可以正常播放。

这是为什么?

4

3 回答 3

0

那么这很容易 - 只需完成我提供的答案的后半部分并将它们写出来。/data/data/ 文件夹永远不会被您的应用程序以外的任何人查看。

于 2013-04-06T21:53:02.157 回答
0

将您的视频放在 res\raw 文件夹中。然后将其复制到视频播放器等应用程序可以读取它们的外部目录。只有您的应用程序可以读取 /data/data/blah....

如果它在 res\raw 中,那么

         typeName = sourceSink.Types.video.toString();
         for (sourceSink.VideoFiles video: sourceSink.VideoFiles.values() ){
             resourceName = video.toString(); 
             fileName = resourceName + ".mp4";
             resource = getResources().getIdentifier(resourceName, "raw", "com.invodo.allshareplay");
             createExternalStoragePublicFile(typeName,fileName,resource);
         }

然后你可以用它来复制它:

        void createExternalStoragePublicFile(String fType, String fname, int res ) {
        // Create a path where we will place our picture in the user's
        // public pictures directory.  Note that you should be careful about
        // what you place here, since the user often manages these files.  For
        // pictures and other media owned by the application, consider
        // Context.getExternalMediaDir().
        File path = null;
        if (((fType.equals(sourceSink.Types.photo.toString())) || (fType.equals(sourceSink.Types.file.toString())) ) ){
            path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);
            }
        if (fType.equals(sourceSink.Types.music.toString())) {
            path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_MUSIC);
            }
        if (fType.equals(sourceSink.Types.video.toString())) {
            path = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_MOVIES);
        }
        File file = new File(path, "/" + fname);



        try {
            // Make sure the Pictures directory exists.
            path.mkdirs();

            // Very simple code to copy a picture from the application's
            // resource into the external file.  Note that this code does
            // no error checking, and assumes the picture is small (does not
            // try to copy it in chunks).  Note that if external storage is
            // not currently mounted this will silently fail.
            InputStream is = getResources().openRawResource(res);
            OutputStream os = new FileOutputStream(file);
            byte[] data = new byte[is.available()];
            is.read(data);
            os.write(data);
            is.close();
            os.close();

            scanMedia(file);

        } catch (IOException e) {
            // Unable to create file, likely because external storage is
            // not currently mounted.
            Log.w("ExternalStorage", "Error writing " + file, e);
        }
    }
于 2013-04-05T19:33:25.280 回答
0

将您的视频放在资产文件夹中,并使用此代码通过 MediaPlayer 播放视频

InputStream is = getResources().getAssets().open("video.mpg");

或者

将您的视频放在资产文件夹中,然后...

String fpath = "/data/data/my.package.org/assets/video.mpg"
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(fpath), "video/*");
于 2013-04-05T14:10:35.500 回答