android之视频的播放(VedioView,SuefaceView)和图片的获得

本文介绍了在Android中如何实现视频播放,包括使用VideoView的简单方法和通过SurfaceView结合MediaPlayer的进阶方式。同时,也详细讲解了如何获取图片,包括即时拍摄和从手机相册选择两种途径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、视频播放

方式一:直接用VideoView
布局文件:

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity">
<Button
    android:id="@+id/button_start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="播放"/>
<VideoView
    android:id="@+id/videoview"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"/>
</LinearLayout>

Activity

public class MainActivity extends Activity {
    private Button mButton;
    private VideoView mVideoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.button_start);
        mVideoView = (VideoView) findViewById(R.id.videoview);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mVideoView.setVideoPath(Environment.getExternalStorageDirectory() + "/shi.3gp");//找到sdcard下的shi.3gp的视频
                mVideoView.setMediaController(new MediaController(MainActivity.this));
                mVideoView.start();
            }
        });
    }

}

方式二:SurfaceView和Media
布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="bofang"/>
    <SurfaceView
        android:id="@+id/surfaceview"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

Activity

public class SurfaceViewActivity extends Activity {
    private Button mButton;
    private SurfaceView mSurfaceViw;
    private MediaPlayer player;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_surfaceview);
        mButton = (Button) findViewById(R.id.button);
        mSurfaceViw = (SurfaceView) findViewById(R.id.surfaceview);
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(player==null){
                    player = new MediaPlayer();
                }
                player.reset();//记得要重置
                try {
                    //设置视频地址
                    player.setDataSource(Environment.getExternalStorageDirectory()+"/DCIM/hh.3gp");//视频来源是sdcard下的DCIM中的hh.3gp文件
                    player.setAudioStreamType(AudioManager.STREAM_MUSIC);//设置播放声音类型
                    player.setDisplay(mSurfaceViw.getHolder());//设置视频播放位置
                    player.prepareAsync();//准备
                    player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mp) {
                            mp.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

图片的获得(1、即拍即得2、从手机相册中获得)

布局文件:

<LinearLayout 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"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="拍照" />
 <Button
        android:id="@+id/button_getpicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="得到相册里的图片"
        android:layout_alignParentRight="true"/>

<ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
/>

</LinearLayout>
public class MainActivity extends Activity {
    private Button mButton;
    private ImageView imageView;
    private File file;
      private  Button mButtonGet;
    public static final int GETPIC_FROM_CAMERA=0x23;
    public static final int GETPIC_FROM_GELLAY=0x24;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mButton = (Button) findViewById(R.id.button);
        imageView = (ImageView) findViewById(R.id.imageview);
         mButtonGet = (Button) findViewById(R.id.button_getpicture);
        mButtonGet.setOnClickListener(new View.OnClickListener() {//得到相册里的照片
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("image/*"); /* 开启Pictures画面Type设定为image */
                file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
                if (!file.exists()) {
                    try {
                        file.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//设置照片存放的位置
                startActivityForResult(intent,GETPIC_FROM_GELLAY);
            }
        });
        //从应用中拍照得到图片
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//隐式启动系统相机
                file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".jpg");
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));//设置照片存放的位置
                startActivityForResult(intent, 0x23);//开始启动
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
         if (resultCode == RESULT_OK) {
            switch (requestCode){
                case GETPIC_FROM_CAMERA:
                    ImageZip.zipImage(file.getAbsolutePath());
                    imageView.setImageURI(Uri.fromFile(file));//得到图片
                    break;
                case GETPIC_FROM_GELLAY:
                    Uri uri = data.getData();
                    imageView.setImageURI(uri);
                    break;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值