Android调用系统多媒体库
这是我封装的一个多媒体工具类,所有调用都是基于系统多媒体库的!
多媒体工具类:package com.team618.mediatest.cn;
import java.io.ByteArrayOutputStream; import java.io.File; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.text.format.Time; public class MediaFactory implements Constant{ static String nowtime = null; public static String mMediaFilePath; public static void takePhoto(Context context,String Path)//拍照功能 { getTime(); mMediaFilePath=Path+nowtime+".jpg"; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( Path, nowtime+".jpg"))); ((Activity)context).startActivityForResult(intent,PHOTOHRAPH); } public static void takePViedo(Context context,String Path)//摄像功能 { Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); ((Activity)context).startActivityForResult(intent, MEDIAHRAPH); } public static void takeAudio(Context context,String Path)//录音功能 { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("audio/*"); ((Activity)context).startActivityForResult(intent, MEDIAHRAPH); } /** @describe 这是一个创建多媒体文件的方法,可以调用系统封装的功能,在进行拍照,录音或者录像 @param 第一个参数是多媒体的类型,第二个参数是存储路径,第三个是文件的名字,第四个是文件的类型 例如: 图片类型 Media_Type="MediaStore.ACTION_IMAGE_CAPTURE" ,此时的文件类型一般为:File_Type=".jpg" 录音类型 Media_Type="MediaStore.Audio.Media.RECORD_SOUND_ACTION" ,此时的文件类型一般为:File_Type=".3gpp" 视频类型 Media_Type="MediaStore.ACTION_VIDEO_CAPTURE" ,此时的文件类型一般为:File_Type=".m4v"; @return 没有返回值 @throw 没有异常 */ /* private static void takeMedia(Context context,String Media_Type,String Path,String File_Name,String File_Type) { Intent intent = new Intent(Media_Type); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File( Path, File_Name+ File_Type))); ((Activity)context).startActivityForResult(intent, MEDIAHRAPH); } */ /** @describe 这是一个图像的裁剪功能,这个方法可以启动系统自带的相册,然后我们选择其中的照片进行裁剪 当裁剪完毕后,就会关闭系统的Activity,并返回处理后的图片的缓存 @param 没有参数传入 @return 返回值是一个处理过以后的位图Bitmap @throw 没有异常 */ public static void startPhotoZoom(Context context){ //图像裁剪,带有照片的选择功能, Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType(IMAGE_UNSPECIFIED); //设置类型 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 64); intent.putExtra("outputY", 64); intent.putExtra("return-data", true); ((Activity)context).startActivityForResult(intent, PHOTORESOULT); // } public static Bitmap getZoomphoto(Intent data) //图片裁剪的辅助方法,用来获得处理以后的结果照片 { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件 return photo; // imageView.setImageBitmap(photo); } else { return null; } } /** * 多媒体文件的选择方法,参数闯入是需要查找的多媒体文件类型,返回值是该多媒体的Uri * * */ public static void selectMedia(Context context,String MEDIA_UNSPECIFIED) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,MEDIA_UNSPECIFIED ); ((Activity)context).startActivityForResult(intent, SELECTMEDIA); } public static void selectAudio(Context context,String MEDIA_UNSPECIFIED) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("audio/*"); //String AUDIO_AMR = "audio/amr"; intent.setClassName("com.android.soundrecorder","com.android.soundrecorder.SoundRecorder"); ((Activity) context).startActivityForResult(intent,SELECTADUIO); } /*Uri的作用 * Cursor cursor = getContentResolver().query(uri, null, null, null, null); cursor.moveToFirst(); String imgNo = cursor.getString(0); // 图片编号 String imgPath = cursor.getString(1); // 图片文件路径 String imgSize = cursor.getString(2); // 图片大小 String imgName = cursor.getString(3); // 图片文件名 */ private static void getTime(){ Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。 t.setToNow(); // 取得系统时间。 int year = t.year; int month = t.month+1; int date = t.monthDay; int hour = t.hour; // 0-23 int minute = t.minute; int second = t.second; nowtime=String.valueOf(year)+String.valueOf(month) +String.valueOf(date)+String.valueOf(hour) + String.valueOf(minute)+String.valueOf(second); } }
主Activity
package com.team618.mediatest.cn; import java.io.File; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MediaTestActivity extends Activity implements Constant { /** Called when the activity is first created. */ public File mMediaFile=null; //多媒体文件 public Uri mMediaUri=null; //多媒体文件的uri public Bitmap mZoomPhoto=null; //缩放后的图片文件 public String Path="/sdcard"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.media_test); Button takePhoto = (Button) findViewById(R.id.takePhoto);//拍照按钮 Button takeViedo = (Button) findViewById(R.id.takeViedo);//摄像按钮 Button takeAudio = (Button) findViewById(R.id.takeAudio);//录音按钮 Button selectPhoto = (Button) findViewById(R.id.selectPhoto);//选择多媒体文件按钮 Button zoomPhoto = (Button) findViewById(R.id.zoomPhoto);//图片裁减按钮 takePhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MediaFactory.takePhoto(MediaTestActivity.this,Path); } }); takeViedo.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MediaFactory.takePViedo(MediaTestActivity.this,Path); } }); takeAudio.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MediaFactory.takeAudio(MediaTestActivity.this,Path); } }); selectPhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MediaFactory.selectMedia(MediaTestActivity.this,IMAGE_UNSPECIFIED); //MediaFactory.selectAudio(MediaTestActivity.this,IMAGE_UNSPECIFIED); } }); zoomPhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub MediaFactory.startPhotoZoom(MediaTestActivity.this); } }); } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; //���� if (requestCode == PHOTOHRAPH){ mMediaFile=new File(MediaFactory.mMediaFilePath); } //�����ý���ļ� if (requestCode == MEDIAHRAPH){ Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null); cursor.moveToFirst(); String mediaFilePath = cursor.getString(1); // ͼƬ�ļ�·�� mMediaFile=new File(mediaFilePath); } // ��ȡ���ͼƬ������Ƭ��Uri����Ƶ��Uri if (requestCode == SELECTMEDIA) { mMediaUri=data.getData(); Cursor cursor = getContentResolver().query(mMediaUri, null, null, null, null); cursor.moveToFirst(); String mediaFilePath = cursor.getString(1); // ͼƬ�ļ�·�� Log.i("path",mediaFilePath); } if (requestCode == SELECTADUIO) { mMediaUri=data.getData(); Log.i("path",mMediaUri.toString());//����ļ���·�� } // �õ�ͼƬ���Ŵ����� if (requestCode == PHOTORESOULT) { mZoomPhoto=MediaFactory.getZoomphoto(data); } super.onActivityResult(requestCode, resultCode, data); } }
常量类
package com.team618.mediatest.cn; public interface Constant { public static final int NONE=0; public static final int PHOTOHRAPH = 1;// 创建多媒体文件 public static final int MEDIAHRAPH = 2;// 创建多媒体文件 public static final int SELECTMEDIA =3; // 选择多媒体文件 public static final int SELECTADUIO=4; public static final int PHOTORESOULT=5;// 图片截图处理结果 public static final String IMAGE_UNSPECIFIED = "image/*"; public static final String VIEDO_UNSPECIFIED = "viedo/*"; public static final String AUDIO_UNSPECIFIED = "audio/*"; }
xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" android:id="@+id/takePhoto"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="视屏" android:id="@+id/takeViedo"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="录音" android:id="@+id/takeAudio"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="找图片" android:id="@+id/selectPhoto"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="截图" android:id="@+id/zoomPhoto" /> </LinearLayout>
注意添加录音权限 <uses-permission android:name="android.permission.RECORD_AUDIO">
</uses-permission>
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28