MediaStore类使用Intent录制音频,拍照

   由于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"/>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值