如何将视频放入apk?

时间:2012-01-10 15:27:25

标签: java android

我想在我的应用中添加视频。但我有一个问题。我读了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();

        }
    }
}

1 个答案:

答案 0 :(得分:2)

您应该将原始资源放入/ res / raw文件夹,然后重建您的R类 并从那里得到它们。

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

摘录:

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

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