如何在android中实现资源的switch语句?

问题描述 投票:-1回答:1

此功能用于返回单击某些图像时要播放的视频URL

public String alphabetf(int a){
        String video_url =new String();
  switch (a){
            case R.drawable.a:
                video_url= "android.resource://" + getPackageName() + "/" + R.raw.a;
                break;
            case R.drawable.b:
                video_url = "android.resource://" + getPackageName() + "/" + R.raw.b;
                break;



                default:
                    video_url = "android.resource://" + getPackageName() + "/" + R.raw.a;


        }


        return video_url;



    }

调用函数。

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_view);
        VideoView mVideoView =(VideoView)findViewById(videoView) ;
        mVideoView.setZOrderOnTop(true);
        Intent intent = getIntent();
        String alphabet = intent.getStringExtra("alphabetName").toString();
        int imageId =intent.getIntExtra("imageId",0);
        Log.w("myApp", alphabet);
        Log.w("id", String.valueOf(imageId));

        Uri videoUri;
        videoUri = Uri.parse(alphabetf(imageId));
        MediaController mediaController= new MediaController(this);
        mediaController.setAnchorView(mVideoView);
        mVideoView.setMediaController(mediaController);
        mVideoView.setVideoURI(videoUri);
        mediaController.requestFocus();
        mVideoView.start();





    }

以上功能仅返回默认值

错误:

W/MediaPlayer: Couldn't open : java.io.FileNotFoundException: No content provider: 
W/VideoView: Unable to open content: 
    java.io.IOException: setDataSource failed.

我正在使用cardview,并且我想在新活动中打开与图像相关的视频。

java android android-studio switch-statement android-cardview
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.