通过Intent传递对Raw视频文件的引用,并在另一个活动中使用它



在我当前的工作代码中,我从显示一些缩略图的屏幕中传递图像的引用。选择缩略图后,将存储图像id,然后在新活动中使用。现在,当选择缩略图时,我想存储图像id和对相关视频文件的引用。

当缩略图被选中时,它会打开一个新的活动,显示带有播放按钮的完整图像。当选择播放按钮时,我希望它调用一个新的活动,该活动将根据缩略图屏幕上保存的引用播放视频。我有一切工作,除了让视频播放使用参考。当我在中硬编码文件名时,视频将播放。

这是我的一些代码片段。

来自缩略图类:

public void onClick(View v) {
    Intent fullImage = new Intent(standard_main.this, full_image.class);
    Intent playVideo = new Intent(standard_main.this, video.class);
    switch(v.getId()){
    case R.id.img_standard1:
        fullImage.putExtra("Full", R.drawable.img_standard1);
        playVideo.putExtra("VideoFile", R.raw.standard); // added to try to reference video for future use
        startActivity(fullImage);
        break;

来自full_image类:

if (x >= leftPlayPoint && x < rightPlayPoint && y >= topPlayPoint && y < bottomPlayPoint){
            startActivity(new Intent("com.example.PLAYVIDEO"));

来自我的视频课程:

VideoView vd = (VideoView) findViewById(R.id.VideoView);
    int vidID = getIntent().getIntExtra("VideoFile",0);
    Uri uri =  Uri.parse("android.resource://com.example/"+ R.raw.standard);

我想以某种方式使用vidID来取代R.raw.standard.

如果我理解正确的话,我不能使用vidID,因为它是Int,Uri.parse想要一个字符串。我尝试使用toString()将Int转换为String,但我怀疑这只是将实际数字转换为字符串,并引入了实际的文件名。我还尝试了String.valueOf(vidID).

我认为Parcelabel可能在某个地方使用,但我不知道如何使用它。我认为的另一个选项是将所有视频名称存储在一个数组中,并以某种方式使用它在video.java文件上动态创建文件名。

我会继续寻找,但我们非常感谢您的帮助。

这是一种方法。也许有更简单的方法,但我不知道。

一些示例代码。

int myResourceId = getIntent().getIntExtra("AudioFile", 0);
StringBuilder sb = new StringBuilder();
// get full resource path, e.g. res/raw/audio.mp3
sb.append(getResources().getString(myResourceId));
// delete res folder
sb.delete(0, sb.indexOf("/"));
// delete file extension
sb.delete(sb.indexOf(".mp3"), sb.length());
// insert package at beginning
sb.insert(0, "android.resource://com.example");
Uri uri = Uri.parse(sb.toString());

最新更新