为了账号安全,请及时绑定邮箱和手机立即绑定

调用系统播放器,调用系统Camera

标签:
Android

1、调用系统音乐播放器

[java] view plaincopy在CODE上查看代码片派生到我的代码片

  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.           

  8.     }  

或者

[java] view plaincopy在CODE上查看代码片派生到我的代码片

  1. Intent it = new Intent(Intent.ACTION_VIEW);  

  2.         it.setDataAndType(Uri.parse("/sdcard/111.mp3"), "audio/mp3");  

  3.         startActivity(it);  

2、调用系统视频播放器

[java] view plaincopy在CODE上查看代码片派生到我的代码片

  1. private void playVideo(String videoPath){  

  2.            Intent intent = new Intent(Intent.ACTION_VIEW);  

  3.            String strend="";  

  4.            if(videoPath.toLowerCase().endsWith(".mp4")){  

  5.                strend="mp4";  

  6.            }  

  7.            else if(videoPath.toLowerCase().endsWith(".3gp")){  

  8.                strend="3gp";  

  9.            }  

  10.            else if(videoPath.toLowerCase().endsWith(".mov")){  

  11.                strend="mov";  

  12.            }  

  13.            else if(videoPath.toLowerCase().endsWith(".wmv")){  

  14.                strend="wmv";  

  15.            }  

  16.              

  17.            intent.setDataAndType(Uri.parse(videoPath), "video/"+strend);  

  18.            startActivity(intent);  

  19.        }  

或者

[java] view plaincopy在CODE上查看代码片派生到我的代码片

  1. Intent it = new Intent(Intent.ACTION_VIEW);  

  2.         it.setDataAndType(Uri.parse("/sdcard/1122.mp4"), "video/mp4");  

  3.         startActivity(it);  

播放来自网络多媒体文件

[java] view plaincopy在CODE上查看代码片派生到我的代码片

  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 plaincopy在CODE上查看代码片派生到我的代码片

  1. public class SysCamera extends Activity {  

  2.   

  3.     public static final int MEDIA_TYPE_IMAGE = 1;  

  4.     public static final int MEDIA_TYPE_VIDEO = 2;  

  5.   

  6.     private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;  

  7.     private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;  

  8.   

  9.     private Uri fileUri;  

  10.     Button btvideo,btphoto;  

  11.       

  12.     @Override  

  13.     public void onCreate(Bundle savedInstanceState) {  

  14.         super.onCreate(savedInstanceState);  

  15.           

  16.         setContentView(R.layout.syscamera);  

  17.          

  18.         btphoto=(Button)findViewById(R.id.sysPhoto);  

  19.         btvideo=(Button)findViewById(R.id.sysVideo);  

  20.         btphoto.setOnClickListener(new Monitor());  

  21.         btvideo.setOnClickListener(new Monitor());  

  22.           

  23.     }  

  24.       

  25.     private void PhotoIntent(){  

  26.          // create Intent to take a picture   

  27.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  

  28.   

  29.         fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image  

  30.         intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name  

  31.         // start the image capture Intent  

  32.         startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);  

  33.     }  

  34.       

  35.     private void VideoIntent(){  

  36.          //create new Intent  

  37.         Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);  

  38.   

  39.         fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);  // create a file to save the video  

  40.         intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);  // set the image file name  

  41.         intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1); // set the video image quality to high  

  42.         // start the Video Capture Intent  

  43.         startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);  

  44.     }  

  45.       

  46.     /** Create a file Uri for saving an image or video */  

  47.     private static Uri getOutputMediaFileUri(int type){  

  48.           return Uri.fromFile(getOutputMediaFile(type));  

  49.     }  

  50.   

  51.     /** Create a File for saving an image or video */  

  52.     private static File getOutputMediaFile(int type){  

  53.         if (Environment.getExternalStorageState() == null){  

  54.             return null;  

  55.         }   

  56.         File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(  

  57.                   Environment.DIRECTORY_PICTURES), "MyCameraApp");  

  58.          

  59.         // Create the storage directory if it does not exist  

  60.         if (! mediaStorageDir.exists()){  

  61.             if (! mediaStorageDir.mkdirs()){  

  62.                 Log.d("MyCameraApp", "failed to create directory");  

  63.                 return null;  

  64.             }  

  65.         }  

  66.   

  67.         // Create a media file name  

  68.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());  

  69.         File mediaFile;  

  70.         if (type == MEDIA_TYPE_IMAGE){  

  71.             mediaFile = new File(mediaStorageDir.getPath() + File.separator +  

  72.             "IMG_"+ timeStamp + ".jpg");  

  73.         } else if(type == MEDIA_TYPE_VIDEO) {  

  74.             mediaFile = new File(mediaStorageDir.getPath() + File.separator +  

  75.             "VID_"+ timeStamp + ".mp4");  

  76.         } else {  

  77.             return null;  

  78.         }  

  79.   

  80.         return mediaFile;  

  81.     }  

  82.       

  83.      @Override  

  84.      public void onBackPressed() {  

  85.             Intent in=new Intent(this, MyRecorderActivity.class);  

  86.             startActivity(in);  

  87.             super.onBackPressed();  

  88.         }  

  89.        

  90.     @Override  

  91.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

  92.         if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {  

  93.             if (resultCode == RESULT_OK) {  

  94.                 // Image captured and saved to fileUri specified in the Intent  

  95.                 Toast.makeText(this, "Image saved to:\n" +  

  96.                          data.getData(), Toast.LENGTH_LONG).show();  

  97.             } else if (resultCode == RESULT_CANCELED) {  

  98.                 // User cancelled the image capture  

  99.             } else {  

  100.                 // Image capture failed, advise user  

  101.             }  

  102.         }  

  103.   

  104.         if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {  

  105.             if (resultCode == RESULT_OK) {  

  106.                 // Video captured and saved to fileUri specified in the Intent  

  107.                 Toast.makeText(this, "Video saved to:\n" +  

  108.                          data.getData(), Toast.LENGTH_LONG).show();  

  109.             } else if (resultCode == RESULT_CANCELED) {  

  110.                 // User cancelled the video capture  

  111.             } else {  

  112.                 // Video capture failed, advise user  

  113.             }  

  114.         }  

  115.     }  

  116.       

  117.     class Monitor implements OnClickListener{  

  118.   

  119.         @Override  

  120.         public void onClick(View v) {  

  121.               

  122.                 switch(v.getId()){  

  123.                 case R.id.sysPhoto:  

  124.                     PhotoIntent();  

  125.                     break;  

  126.                 case R.id.sysVideo:  

  127.                     VideoIntent();  

  128.                     break;  

  129.                 }  

  130.         }  

  131.     }  

  132. }  

原文链接:http://blog.csdn.net/yudajun/article/details/7752226

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消