如何使用 intent.putextra 设置 videoview 资源
Posted
技术标签:
【中文标题】如何使用 intent.putextra 设置 videoview 资源【英文标题】:How to set videoview resource using intent.putextra 【发布时间】:2018-10-18 03:07:41 【问题描述】:我的原始文件夹中有一些 mp4 视频,现在我想使用 intent 将这些视频传递到我的 videoview。当用户点击某个项目时,将额外内容从活动 A 放入活动 B。
这是我的适配器类:
public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder>
List<abs.Ex> exList;
Context context;
String ex1 = "android.resource://" + context.getPackageName() + "/" + R.raw.ex1;
absadapter(List exList,Context ctx)
this.exList= exList;
context=ctx;
public static class exViewHolder extends RecyclerView.ViewHolder
CardView cardView;
TextView exname;
ImageView exlogo;
exViewHolder(View itemView)
super(itemView);
cardView= itemView.findViewById(R.id.cardView);
exname= itemView.findViewById(R.id.ex_name);
exlogo=itemView.findViewById(R.id.exlogo);
@Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
return evh;
@Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position)
holder.exname.setText(exList.get(position).name);
holder.exlogo.setImageResource(exList.get(position).logoId);
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (position == 0 )
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.crunches);
context.startActivity(i1);
if (position == 1)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.raw.ex1);
i1.putExtra("text",R.string.declinecrunch);
context.startActivity(i1);
if (position == 2)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.dumsidebend);
context.startActivity(i1);
if (position == 3)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.hanglegraise);
context.startActivity(i1);
if (position == 4)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.inclinelegraisegi);
i1.putExtra("text",R.string.inclineleg);
context.startActivity(i1);
if (position == 5)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.legraises);
i1.putExtra("text",R.string.legraise);
context.startActivity(i1);
if (position == 6)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.flatbenchlyingleg);
context.startActivity(i1);
if (position == 7)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.jackknife);
context.startActivity(i1);
if (position == 8)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.jackknifegif);
i1.putExtra("text",R.string.jackknife);
context.startActivity(i1);
if (position == 9)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.twisthip);
i1.putExtra("text",R.string.twisthip);
context.startActivity(i1);
if (position == 10)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.weightcrunch);
context.startActivity(i1);
if (position == 11)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.plank);
context.startActivity(i1);
if (position == 12)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.sideplank);
context.startActivity(i1);
if (position == 13)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.superman);
context.startActivity(i1);
if (position == 14)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.twistcrunch);
context.startActivity(i1);
);
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
super.onAttachedToRecyclerView(recyclerView);
@Override
public int getItemCount()
return exList.size();
我想在项目点击视频源上做bindviewholder内的所有代码:
这是我的主要课程:
public class abdetails extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abdetails);
VideoView videoView;
videoView=findViewById(R.id.vdvw);
Intent i1=getIntent();
TextView tv;
tv = findViewById(R.id.exdesc);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setText(i1.getIntExtra("text",0));
【问题讨论】:
【参考方案1】:您不应传递整个视频。只需传递视频 ID(或视频名称,...),然后在活动 B 中,再次访问 raw
文件夹即可获取目标视频
在您的适配器中,传递位置:
@Override
public void onBindViewHolder(final Absadapter.ExViewHolder holder, final int position)
...
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent i1=new Intent(context, Abdetails.class);
i1.putExtra("POSITION", position);
context.startActivity(i1);
...
然后在你的 AbDetailActivity 中:
public class AbDetailActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abdetails);
if (getIntent() != null && getIntent().getExtras() != null)
Bundle bundle = getIntent().getExtras();
int position = bundle.getInt("POSITION", 0);
switch (position)
case 0:
// Load video 0
break;
case 1:
// Load video 1
break:
...
【讨论】:
这没用,你能告诉我如何编码我被 uri.parse 卡住,因为 intent.getextra 不接受它 R.raw.ex1 是一个资源文件,因此您可以在任何有上下文的地方访问它! @RuchitPatel【参考方案2】:我已经编辑了您的代码。希望它能解决你的问题。单击第一项时,下面的代码将播放您的视频文件。我的意思是当位置 == 0;
public class absadapter extends RecyclerView.Adapter<absadapter.exViewHolder>
List<abs.Ex> exList;
Context context;
String ex1 = "android.resource://" + context.getPackageName() + "/" + R.raw.ex1;
absadapter(List exList,Context ctx)
this.exList= exList;
context=ctx;
public static class exViewHolder extends RecyclerView.ViewHolder
CardView cardView;
TextView exname;
ImageView exlogo;
exViewHolder(View itemView)
super(itemView);
cardView= itemView.findViewById(R.id.cardView);
exname= itemView.findViewById(R.id.ex_name);
exlogo=itemView.findViewById(R.id.exlogo);
@Override
public absadapter.exViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
View viewthigh= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_abslayout,parent,false);
absadapter.exViewHolder evh=new absadapter.exViewHolder(viewthigh);
return evh;
@Override
public void onBindViewHolder(final absadapter.exViewHolder holder, final int position)
holder.exname.setText(exList.get(position).name);
holder.exlogo.setImageResource(exList.get(position).logoId);
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
if (position == 0 )
Intent i1=new Intent(context, abdetails.class);
i1.putExtra("video", ex1);
context.startActivity(i1);
if (position == 1)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.raw.ex1);
i1.putExtra("text",R.string.declinecrunch);
context.startActivity(i1);
if (position == 2)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.dumsidebend);
context.startActivity(i1);
if (position == 3)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.hanglegraise);
context.startActivity(i1);
if (position == 4)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.inclinelegraisegi);
i1.putExtra("text",R.string.inclineleg);
context.startActivity(i1);
if (position == 5)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.legraises);
i1.putExtra("text",R.string.legraise);
context.startActivity(i1);
if (position == 6)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.flatbenchlyingleg);
context.startActivity(i1);
if (position == 7)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.jackknife);
context.startActivity(i1);
if (position == 8)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.jackknifegif);
i1.putExtra("text",R.string.jackknife);
context.startActivity(i1);
if (position == 9)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("img",R.drawable.twisthip);
i1.putExtra("text",R.string.twisthip);
context.startActivity(i1);
if (position == 10)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.weightcrunch);
context.startActivity(i1);
if (position == 11)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.plank);
context.startActivity(i1);
if (position == 12)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.sideplank);
context.startActivity(i1);
if (position == 13)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.superman);
context.startActivity(i1);
if (position == 14)
Intent i1=new Intent(context,abdetails.class);
i1.putExtra("text",R.string.twistcrunch);
context.startActivity(i1);
);
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView)
super.onAttachedToRecyclerView(recyclerView);
@Override
public int getItemCount()
return exList.size();
这是您的视频播放器活动。
public class abdetails extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abdetails);
VideoView videoView;
videoView=findViewById(R.id.vdvw);
Intent i1=getIntent();
TextView tv;
tv = findViewById(R.id.exdesc);
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setText(i1.getIntExtra("text",0));
if(i1 != null)
String path = i1.getStringExtra("video", "");
videoView.setVideoURI(Uri.parse(path));
videoView.start();
videoView.setVisibility(View.VISIBLE);
【讨论】:
以上是关于如何使用 intent.putextra 设置 videoview 资源的主要内容,如果未能解决你的问题,请参考以下文章
intent.putExtra 的所有属性以在 android 中裁剪图像
Android 中使用intent.putExtra实现Activity之间的参数传递
Intent.putExtra(String,Serializable)出错,请大家帮忙看看