如何将视频放入apk文件中?

3

我想在我的应用程序中添加视频。但是我遇到了问题。我阅读了Android开发者文档并找到了Java代码示例,但我不明白如何将视频放入APK中,路径应该是什么?我意识到视频应该放在SD卡上,对吗?我很困惑。 Android开发者提供的VideoView.java:

/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.media;

import com.example.android.apis.R;
import android.app.Activity;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoViewDemo extends Activity {

    /**
     * TODO: Set the path variable to a streaming video URL or a local media
     * file path.
     */
    private String path = "";
    private VideoView mVideoView;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.videoview);
        mVideoView = (VideoView) findViewById(R.id.surface_view);

        if (path == "") {
            // Tell the user to provide a media file URL/path.
            Toast.makeText(
                    VideoViewDemo.this,
                    "Please edit VideoViewDemo Activity, and set path"
                            + " variable to your media file URL/path",
                    Toast.LENGTH_LONG).show();

        } else {

            /*
             * Alternatively,for streaming media you can use
             * mVideoView.setVideoURI(Uri.parse(URLstring));
             */
            mVideoView.setVideoPath(path);
            mVideoView.setMediaController(new MediaController(this));
            mVideoView.requestFocus();

        }
    }
}

你可以将视频放在资产文件夹中。 - sebataz
这里有一个可能有用的SO讨论:https://dev59.com/aHA75IYBdhLWcg3w7tx6 - kosa
这是一个例子。 - Suragch
1个回答

2
你应该将原始资源放入 /res/raw 文件夹中,然后重新构建 R 类并从那里获取它们。

http://developer.android.com/guide/topics/resources/providing-resources.html

摘录:

原始/
以原始形式保存的任意文件。要使用原始InputStream打开这些资源,请使用资源ID调用Resources.openRawResource(),该ID为R.raw.filename。

但是,如果您需要访问原始文件名和文件层次结构,则可以考虑将某些资源保存在assets/目录中(而不是res/raw/)。 assets/中的文件没有分配资源ID,因此只能使用AssetManager读取它们。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接