Passing a reference to a Raw video file through Intent and using it in another activity
In my current working code, I pass the reference of an image from a screen that shows some thumbnails. When the thumbnail is selected, the image id is stored and then used in a new activity. Now, when a thumbnail is selected, I want to store the image id and a reference to the associated video file.
When the thumbnail is selected, it opens a new activity that shows the full image with a play button. When the play button is selected, I want it to call a new activity that will play the video based on the reference saved on the thumbnail screen. I have everything working except getting the video to play using the reference. When I hard code the file name in, the video will play.
here's some snippets of my code.
From thumbnail class:
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;
From full_image class:
if (x >= leftPlayPoint && x < rightPlayPoint && y >= topPlayPoint && y < bottomPlayPoint){
startActivity(new Intent("com.example.PLAYVIDEO"));
From my video class:
VideoView vd = (VideoView) findViewById(R.id.VideoView);
int vidID = getIntent().getIntExtra("VideoFile",0);
Uri uri = Uri.parse("android.resource://com.example/"+ R.raw.standard);
I want to somehow use vidID to replace R.raw.standard.
If I'm understanding correctly, I cannot use vidID because it is an Int and Uri.parse wants a string. I tried converting the Int to a String using toString() but I suspect that only converted the actual number to a string and did bring in the actual file name. I also tried String.valueOf(vidID).
I'm thinking that Parcelabel might be used somewhere, but I'm not following 开发者_如何学编程how to use it. One other option I thought was to store all the video names in an array and somehow use this to dynamically create the file name on the video.java file.
I'll keep searching, but any help is much appreciated.
This is one way to do it. Perhaps there is an easier way, but none that I know of.
Some example code.
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());
精彩评论