[英]Passing a reference to a Raw video file through Intent and using it in another activity
在我當前的工作代碼中,我從顯示一些縮略圖的屏幕傳遞圖像的引用。 選擇縮略圖后,將圖像ID存儲,然后在新活動中使用。 Now, when a thumbnail is selected, I want to store the image id and a reference to the associated video file.
選擇縮略圖后,它會打開一個新活動,其中顯示帶有播放按鈕的完整圖像。 選擇播放按鈕后,我希望它調用一個新活動,該活動將根據縮略圖屏幕上保存的參考播放視頻。 除了使用參考播放視頻外,我一切正常。 當我硬編碼文件名時,視頻將播放。
這是我的代碼的一些片段。
從縮略圖 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;
來自 full_image class:
if (x >= leftPlayPoint && x < rightPlayPoint && y >= topPlayPoint && y < bottomPlayPoint){
startActivity(new Intent("com.example.PLAYVIDEO"));
從我的視頻 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);
我想以某種方式使用 vidID 來替換 R.raw.standard。
如果我理解正確,我不能使用 vidID 因為它是一個 Int 並且 Uri.parse 需要一個字符串。 我嘗試使用 toString() 將 Int 轉換為字符串,但我懷疑僅將實際數字轉換為字符串並確實引入了實際文件名。 我也試過 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());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.