![](/img/trans.png)
[英]Launch gallery with Images and Videos get back the selected image/video - Android
[英]Correct intent to launch gallery and get uri for selected video to play in VideoView
我的UI有ImageButton,我用它來啟動畫廊,我想從中選擇要在videoview中播放的視頻。 我使用的代碼如下,但它不播放視頻。
public class MainActivity extends Activity {
VideoView videoView;
private static final int PICK_FROM_GALLERY=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final VideoView videoView =(VideoView)findViewById(R.id.videoView1);
MediaController mediaController= new MediaController(this);
mediaController.setAnchorView(videoView);
ImageButton btn1 = (ImageButton)findViewById(R.id.galleryPicker);
btn1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY) {
Uri mVideoURI = data.getData();
videoView.setVideoURI(mVideoURI);
videoView.start(); //edited
}
}
});
}
布局:Activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<VideoView
android:id="@+id/videoView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<ImageButton
android:id="@+id/galleryPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:adjustViewBounds="true"
android:contentDescription="@string/gallery_picker"
android:scaleType="center"
android:src="@drawable/gallery_picker" />
有幫助嗎?
這樣做:
public class MainActivity extends Activity
{
.... //variables declarations here
VideoView videoView;
MediaController mc;
protected void onCreate(Bundle savedInstanceState)
{
........
......
videoView = (VideoView)findViewById(R.id.videoView1);
mc = new MediaController(this);
mc.setAnchorView(videoView);
ImageButton btn1 = (ImageButton)findViewById(R.id.galleryPicker);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Complete action using"),PICK_FROM_GALLERY);
}
} );
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode != RESULT_OK) return;
if (requestCode == PICK_FROM_GALLERY)
{
Uri mVideoURI = data.getData();
videoView.setMediaController(mc);
videoView.setVideoURI(mVideoURI);
videoview.requestFocus();
videoview.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
videoview.start();
}
});
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.