拍摄照片和视频
2016-04-17 13:06
567 查看
在Android多媒体开发中我们最常用的就是照片、视频的拍摄。其实在开发中我们不会自己去写一个照相机、摄像机程序,而是去调用系统中已经存在的照相、摄影程序;
有时我们需要将我们的拍摄的照片先保存到外存,我们可以在Intent 的MediaStore.EXTRA_
OUTPUT 中放入照片保存位置的URI。
更改我们的Activity:
添加权限:
下面的Demo是我们通过上面的Button录制视频,下面可以通过VideoView观看我们的录制的视频。
(1) MediaStore.EXTRA_VIDEO_QUALITY
a.整型值,用来描述拍摄的视频的质量等级。
b. 0 表示低质量,1 表示高质量
(2) MediaStore.EXTRA_OUTPUT
c.保存视频内容的Uri
d. 如果没有指定保存位置,视频则会保存到设备的标准位置中。
视频录制结束后,会在结果Intent 的data 中返回视频数据真正存储位置的Uri,我们通过Uri可以通过VideoView.setVideoURI(data.getData());来播放我们的视频;
对于如何将VideoView与MediaController进行联系,请读者自己查资料吧!在这里不做讲解了!
拍摄照片
1:布局
<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" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt_takepicture" android:text="拍照" android:textSize="20sp" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:id="@+id/iv_showimage" /> </LinearLayout>
2、Activity
package com.example.takethepicture; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private static final int REQUEST_IMAGE = 0x100; private Button bt_take; private ImageView iv_picture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_take = (Button) this.findViewById(R.id.bt_takepicture); iv_picture = (ImageView) this.findViewById(R.id.iv_showimage); bt_take.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_IMAGE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_IMAGE: if(Activity.RESULT_OK == resultCode){ //我们直接从Intent中获取我们的Bitmap Bitmap image = (Bitmap) data.getExtras().get("data"); iv_picture.setImageBitmap(image); } break; } } }
有时我们需要将我们的拍摄的照片先保存到外存,我们可以在Intent 的MediaStore.EXTRA_
OUTPUT 中放入照片保存位置的URI。
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagePath));
更改我们的Activity:
package com.example.takethepicture; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private static final int REQUEST_IMAGE = 0x100; private Button bt_take; private ImageView iv_picture; private File imagePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt_take = (Button) this.findViewById(R.id.bt_takepicture); iv_picture = (ImageView) this.findViewById(R.id.iv_showimage); imagePath = new File(Environment.getExternalStorageDirectory(),"myimage.jpg"); bt_take.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imagePath)); startActivityForResult(intent, REQUEST_IMAGE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_IMAGE: if(Activity.RESULT_OK == resultCode){ Toast.makeText(MainActivity.this, imagePath.getAbsolutePath(), Toast.LENGTH_LONG).show(); //为了避免将全尺寸的Bitmap加载到内存中,需要图片在屏幕上显示之前,通过BitmapFactory.Options对图片进行缩放 BitmapFactory.Options opts = new BitmapFactory.Options(); opts.inSampleSize = 10; Bitmap image = BitmapFactory.decodeFile(imagePath.getAbsolutePath(), opts); iv_picture.setImageBitmap(image); } break; } } }
添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
下面的Demo是我们通过上面的Button录制视频,下面可以通过VideoView观看我们的录制的视频。
拍摄视频
真正的视频片段数据是不会通过Intent的附加数据返回的,通常我们都是保存到一个指定的文件中,以下两个参数可能会作为附加数据添加到Intent中:(1) MediaStore.EXTRA_VIDEO_QUALITY
a.整型值,用来描述拍摄的视频的质量等级。
b. 0 表示低质量,1 表示高质量
(2) MediaStore.EXTRA_OUTPUT
c.保存视频内容的Uri
d. 如果没有指定保存位置,视频则会保存到设备的标准位置中。
视频录制结束后,会在结果Intent 的data 中返回视频数据真正存储位置的Uri,我们通过Uri可以通过VideoView.setVideoURI(data.getData());来播放我们的视频;
1:布局
<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" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="拍摄视频" android:id="@+id/bt_video" /> <VideoView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/videoView" /> </LinearLayout>
2、Activity
package com.example.takevideo; import java.io.File; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.MediaController; import android.widget.Toast; import android.widget.VideoView; public class MainActivity extends Activity { private static final int REQUEST_VIDEO = 0x100; private Button button; private VideoView videoView; private MediaController mediaController; private File videoPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mediaController = new MediaController(MainActivity.this); button = (Button) this.findViewById(R.id.bt_video); videoView = (VideoView) this.findViewById(R.id.videoView); videoPath = new File(Environment.getExternalStorageDirectory(),"myVideo"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(videoPath)); intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); startActivityForResult(intent, REQUEST_VIDEO); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_VIDEO: if(Activity.RESULT_OK == resultCode){ Toast.makeText(MainActivity.this, videoPath.getAbsolutePath(), Toast.LENGTH_LONG).show(); if(videoPath.exists()){ videoView.setVideoPath(videoPath.getAbsolutePath()); videoView.setMediaController(mediaController); mediaController.setMediaPlayer(videoView); videoView.requestFocus(); } //videoView.setVideoURI(data.getData()); } break; } } }
3、添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
对于如何将VideoView与MediaController进行联系,请读者自己查资料吧!在这里不做讲解了!
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories