对话框工具类用法(一)
———选择对话框调用系统视频和即时录像
对话框的用途很广泛,很多时候,这些对话框我们都可以复用,这样能节省下来很多时间,所以今天在这里把关于对话框相关的代码整理一下,以便于日后阅读,日后如果有发现新的用法,也会在这里更新。
<!--[if gte mso 9]><xml><w:WordDocument><w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel><w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery><w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery><w:DocumentKind>DocumentNotSpecified</w:DocumentKind><w:DrawingGridVerticalSpacing>7.8</w:DrawingGridVerticalSpacing><w:View>Normal</w:View><w:Compatibility></w:Compatibility><w:Zoom>0</w:Zoom></w:WordDocument></xml><![endif]--><!--StartFragment--><!--EndFragment-->
今次利用一个调用系统资源和选择对话框结合起来。
/** * 选择本地视频还是录制上传 */ private void showChooseDialog(int type) { AlertDialog.Builder builder = new Builder(context); if (type == Configs.Type.VIDEO) { builder.setTitle("发送视频"); builder.setItems(new String[] { "即时发送", "本地发送" }, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); Intent intent = null; switch (which) { case 0: Intent intent_video = new Intent( MediaStore.ACTION_VIDEO_CAPTURE); intent_video.putExtra( MediaStore.EXTRA_VIDEO_QUALITY, Configs.Format.VIDEO_QUALITY); intent_video.putExtra( MediaStore.EXTRA_SIZE_LIMIT, Configs.Format.VIDEO_SIZE_LIMIT); intent_video.putExtra( MediaStore.EXTRA_DURATION_LIMIT, Configs.Format.LIMIT_VIDEO_DURATION); if (checkMemoryCard()) { startActivityForResult( intent_video, Configs.Code.REQUEST_CODE_TAKE_VEDIO); } break; case 1: intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType( MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "video/*"); startActivityForResult(intent, Configs.Code.REQUEST_CODE_ALBMN_VIDEO); break; } } }); } builder.create().show(); } 在onActivityResult方法里面 case Configs.Code.REQUEST_CODE_TAKE_VEDIO: Uri videoUri = data.getData(); Cursor videoCursor = this.getContentResolver().query(videoUri, null, null, null, null); if (videoCursor != null && videoCursor.moveToNext()) { videoFilePath = videoCursor.getString(videoCursor .getColumnIndex(VideoColumns.DATA)); // int id = videoCursor.getInt(videoCursor // .getColumnIndex(VideoColumns._ID)); // Bitmap videoBitmap = Thumbnails.getThumbnail( // getContentResolver(), id, Thumbnails.MICRO_KIND, // null); videoCursor.close(); } sendFile(to, videoFilePath); videoFilePath = null; break; case Configs.Code.REQUEST_CODE_ALBMN_VIDEO: Uri video_uri = data.getData(); String[] video_proj = { MediaStore.Video.Media.DATA }; Cursor _videoCursor = getContentResolver().query(video_uri, video_proj, null, null, null); if (_videoCursor != null) { int column_index = _videoCursor .getColumnIndexOrThrow(MediaStore.Video.Media.DATA); if (_videoCursor.getCount() > 0 && _videoCursor.moveToFirst()) { videoFilePath = _videoCursor.getString(column_index); sendFile(to, videoFilePath);// 发送视频文件 videoFilePath = null; } else { Toast.makeText(this, "视频未找到", Toast.LENGTH_SHORT) .show(); } } else { Toast.makeText(this, "视频未找到", Toast.LENGTH_SHORT).show(); }