由于MediaStore类支持使用Intent来录制音频和视频。使用Intent录制音频将会更加简单。
<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" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="录制音频" />
</RelativeLayout>
package com.example.mediastore;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startRecording();
}
});
}
protected void startRecording() {
Intent intent = new Intent("android.provider.MediaStore.RECORD_SOUND");
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 0:
if(resultCode == RESULT_OK)
{
Uri recordedAudioPath = data.getData();
Log.v("Demo", "Uri is "+recordedAudioPath.toString());
}
break;
}
}
}
这样就可以实现录制音频功能 了。而且MediaStore活动已经拥有这些权限了,因此应用程序不必拥有这些权限。
下面是使用MediaStore类拍照:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/photo_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拍照"
/>
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:id="@+id/img"
/>
</LinearLayout>
package com.example.mediastore;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class OtherActivity extends Activity{
private Uri myPicture = null;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
img = (ImageView) findViewById(R.id.img);
Button btn = (Button) findViewById(R.id.photo_btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(Media.TITLE, "My demo image");
values.put(Media.DESCRIPTION, "Image Captured by Camera via an Intent");
myPicture = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, myPicture);
startActivityForResult(i, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 0 && resultCode == Activity.RESULT_OK)
{
System.out.println("---------1---------");
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(bitmap);
}
}
}
以上可以实现拍照,拍照需要在清单文件添加相应权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>