---
title: "Android 调用系统播放器，调用系统Camera"
author: "Java-Object"
date: "2014-10-17T15:12:57Z"
tags: []
categories: ["编程语言"]
slug: "android-camera"
draft: false
lastmod: "2014-10-17T15:12:57Z"
id: 2144711
url: "https://hezd.iteye.com/blog/2144711"
---

转载自：[http://blog.csdn.net/yudajun/article/details/7752226](http://blog.csdn.net/yudajun/article/details/7752226)

1、调用系统音乐播放器

 

**\[java]** [view plain](http://blog.csdn.net/yudajun/article/details/7752226 "view plain")[copy](http://blog.csdn.net/yudajun/article/details/7752226 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/338025 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/338025/fork "派生到我的代码片")

 

1. private void playAudio(String audioPath){
2. Intent intent = new Intent();
3. intent.setAction(android.content.Intent.ACTION\_VIEW);
4. intent.setDataAndType(Uri.parse(audioPath), "audio/mp3");
5. intent.setComponent(new ComponentName("com.android.music","com.android.music.MediaPlaybackActivity"));
6. startActivity(intent);
7. }

或者

**\[java]** [view plain](http://blog.csdn.net/yudajun/article/details/7752226 "view plain")[copy](http://blog.csdn.net/yudajun/article/details/7752226 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/338025 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/338025/fork "派生到我的代码片")

 

1. Intent it = new Intent(Intent.ACTION\_VIEW);
2. it.setDataAndType(Uri.parse("/sdcard/111.mp3"), "audio/mp3");
3. startActivity(it);

 

 

2、调用系统视频播放器

 

**\[java]** [view plain](http://blog.csdn.net/yudajun/article/details/7752226 "view plain")[copy](http://blog.csdn.net/yudajun/article/details/7752226 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/338025 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/338025/fork "派生到我的代码片")

 

01. private void playVideo(String videoPath){
02. Intent intent = new Intent(Intent.ACTION\_VIEW);
03. String strend="";
04. if(videoPath.toLowerCase().endsWith(".mp4")){
05. strend="mp4";
06. }
07. else if(videoPath.toLowerCase().endsWith(".3gp")){
08. strend="3gp";
09. }
10. else if(videoPath.toLowerCase().endsWith(".mov")){
11. strend="mov";
12. }
13. else if(videoPath.toLowerCase().endsWith(".wmv")){
14. strend="wmv";
15. }
16. intent.setDataAndType(Uri.parse(videoPath), "video/"+strend);
17. startActivity(intent);
18. }

 

 

或者

 

**\[java]** [view plain](http://blog.csdn.net/yudajun/article/details/7752226 "view plain")[copy](http://blog.csdn.net/yudajun/article/details/7752226 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/338025 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/338025/fork "派生到我的代码片")

 

1. Intent it = new Intent(Intent.ACTION\_VIEW);
2. it.setDataAndType(Uri.parse("/sdcard/1122.mp4"), "video/mp4");
3. startActivity(it);

 

 

播放来自网络多媒体文件

 

**\[java]** [view plain](http://blog.csdn.net/yudajun/article/details/7752226 "view plain")[copy](http://blog.csdn.net/yudajun/article/details/7752226 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/338025 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/338025/fork "派生到我的代码片")

 

1. String extension = MimeTypeMap.getFileExtensionFromUrl(url);
2. String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
3. Intent mediaIntent = new Intent(Intent.ACTION\_VIEW);
4. mediaIntent.setDataAndType(Uri.parse(url), mimeType);
5. startActivity(mediaIntent);

 

 

调用系统Camera 

 

**\[java]** [view plain](http://blog.csdn.net/yudajun/article/details/7752226 "view plain")[copy](http://blog.csdn.net/yudajun/article/details/7752226 "copy")[![在CODE上查看代码片](https://code.csdn.net/assets/CODE_ico.png)](https://code.csdn.net/snippets/338025 "在CODE上查看代码片")[![派生到我的代码片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/338025/fork "派生到我的代码片")

 

001. public class SysCamera extends Activity {
002. public static final int MEDIA\_TYPE\_IMAGE = 1;
003. public static final int MEDIA\_TYPE\_VIDEO = 2;
004. private static final int CAPTURE\_IMAGE\_ACTIVITY\_REQUEST\_CODE = 100;
005. private static final int CAPTURE\_VIDEO\_ACTIVITY\_REQUEST\_CODE = 200;
006. private Uri fileUri;
007. Button btvideo,btphoto;
008. @Override
009. public void onCreate(Bundle savedInstanceState) {
010. super.onCreate(savedInstanceState);
011. setContentView(R.layout.syscamera);
012. btphoto=(Button)findViewById(R.id.sysPhoto);
013. btvideo=(Button)findViewById(R.id.sysVideo);
014. btphoto.setOnClickListener(new Monitor());
015. btvideo.setOnClickListener(new Monitor());
016. }
017. private void PhotoIntent(){
018. // create Intent to take a picture
019. Intent intent = new Intent(MediaStore.ACTION\_IMAGE\_CAPTURE);
020. fileUri = getOutputMediaFileUri(MEDIA\_TYPE\_IMAGE); // create a file to save the image
021. intent.putExtra(MediaStore.EXTRA\_OUTPUT, fileUri); // set the image file name
022. // start the image capture Intent
023. startActivityForResult(intent, CAPTURE\_IMAGE\_ACTIVITY\_REQUEST\_CODE);
024. }
025. private void VideoIntent(){
026. //create new Intent
027. Intent intent = new Intent(MediaStore.ACTION\_VIDEO\_CAPTURE);
028. fileUri = getOutputMediaFileUri(MEDIA\_TYPE\_VIDEO);  // create a file to save the video
029. intent.putExtra(MediaStore.EXTRA\_OUTPUT, fileUri);  // set the image file name
030. intent.putExtra(MediaStore.EXTRA\_VIDEO\_QUALITY, 1); // set the video image quality to high
031. // start the Video Capture Intent
032. startActivityForResult(intent, CAPTURE\_VIDEO\_ACTIVITY\_REQUEST\_CODE);
033. }
034. /\** Create a file Uri for saving an image or video \*/
035. private static Uri getOutputMediaFileUri(int type){
036. return Uri.fromFile(getOutputMediaFile(type));
037. }
038. /\** Create a File for saving an image or video \*/
039. private static File getOutputMediaFile(int type){
040. if (Environment.getExternalStorageState() == null){
041. return null;
042. }
043. File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
044. Environment.DIRECTORY\_PICTURES), "MyCameraApp");
045. // Create the storage directory if it does not exist
046. if (! mediaStorageDir.exists()){
047. if (! mediaStorageDir.mkdirs()){
048. Log.d("MyCameraApp", "failed to create directory");
049. return null;
050. }
051. }
052. // Create a media file name
053. String timeStamp = new SimpleDateFormat("yyyyMMdd\_HHmmss").format(new Date());
054. File mediaFile;
055. if (type == MEDIA\_TYPE\_IMAGE){
056. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
057. "IMG\_"+ timeStamp + ".jpg");
058. } else if(type == MEDIA\_TYPE\_VIDEO) {
059. mediaFile = new File(mediaStorageDir.getPath() + File.separator +
060. "VID\_"+ timeStamp + ".mp4");
061. } else {
062. return null;
063. }
064. return mediaFile;
065. }
066. @Override
067. public void onBackPressed() {
068. Intent in=new Intent(this, MyRecorderActivity.class);
069. startActivity(in);
070. super.onBackPressed();
071. }
072. @Override
073. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
074. if (requestCode == CAPTURE\_IMAGE\_ACTIVITY\_REQUEST\_CODE) {
075. if (resultCode == RESULT\_OK) {
076. // Image captured and saved to fileUri specified in the Intent
077. Toast.makeText(this, "Image saved to:\\n" +
078. data.getData(), Toast.LENGTH\_LONG).show();
079. } else if (resultCode == RESULT\_CANCELED) {
080. // User cancelled the image capture
081. } else {
082. // Image capture failed, advise user
083. }
084. }
085. if (requestCode == CAPTURE\_VIDEO\_ACTIVITY\_REQUEST\_CODE) {
086. if (resultCode == RESULT\_OK) {
087. // Video captured and saved to fileUri specified in the Intent
088. Toast.makeText(this, "Video saved to:\\n" +
089. data.getData(), Toast.LENGTH\_LONG).show();
090. } else if (resultCode == RESULT\_CANCELED) {
091. // User cancelled the video capture
092. } else {
093. // Video capture failed, advise user
094. }
095. }
096. }
097. class Monitor implements OnClickListener{
098. @Override
099. public void onClick(View v) {
100. switch(v.getId()){
101. case R.id.sysPhoto:
102. PhotoIntent();
103. break;
104. case R.id.sysVideo:
105. VideoIntent();
106. break;
107. }
108. }
109. }
110. }