首页 文章 精选 留言 我的

精选列表

搜索[批量导入手机通讯录],共10000篇文章
优秀的个人博客,低调大师

Xamarin.Android 调用手机拍照功能

最近开发Android遇到了调用本地拍照功能,于是在网上搜了一些方法,加上自己理解的注释,在这儿记录下来省的下次用时候找不到,同事也给正在寻找调用本地拍照功能的小伙伴一些帮助~ 实现思路:首先加载-->判断是否具备拍照功能-->创建图片目录(文件夹)-->点击拍照事件-->返回图片并绑定在控件上显示。 引用命名空间: using System; using System.Collections.Generic; using Android.App; using Android.Content; using Android.Content.PM; using Android.Graphics; using Android.OS; using Android.Provider; using Android.Widget; using Java.IO; using Environment = Android.OS.Environment; using Uri = Android.Net.Uri; 加载: protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.Main); if (IsThereAnAppToTakePictures()) //判断本设备是否存在拍照功能 { CreateDirectoryForPictures(); Button button = FindViewById<Button>(Resource.Id.myButton); _imageView = FindViewById<ImageView>(Resource.Id.imageView1); button.Click += TakeAPicture; } } 判断是否具备拍照功能: /// <summary> /// 判断是否具备拍照功能 /// </summary> /// <returns></returns> private bool IsThereAnAppToTakePictures() { Intent intent = new Intent(MediaStore.ActionImageCapture); IList<ResolveInfo> availableActivities = PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly); return availableActivities != null && availableActivities.Count > 0; } 创建图片目录(文件夹): /// <summary> /// 创建目录图片 /// </summary> private void CreateDirectoryForPictures() { App._dir = new File( Environment.GetExternalStoragePublicDirectory( Environment.DirectoryPictures), "CameraAppDemo"); //CameraAppDemo if (!App._dir.Exists()) { App._dir.Mkdirs(); } } 点击拍照事件: /// <summary> /// 拍照 /// </summary> /// <param name="sender"></param> /// <param name="eventArgs"></param> private void TakeAPicture(object sender, EventArgs eventArgs) { Intent intent = new Intent(MediaStore.ActionImageCapture); App._file = new File(App._dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid())); //保存路径 intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file)); StartActivityForResult(intent, 0); } 返回图片并绑定在控件上显示: /// <summary> /// 拍照结束执行 /// </summary> /// <param name="requestCode"></param> /// <param name="resultCode"></param> /// <param name="data"></param> protected override void OnActivityResult(int requestCode, Result resultCode, Intent data) { base.OnActivityResult(requestCode, resultCode, data); Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile); Uri contentUri = Uri.FromFile(App._file); mediaScanIntent.SetData(contentUri); SendBroadcast(mediaScanIntent); // Display in ImageView. We will resize the bitmap to fit the display // Loading the full sized image will consume to much memory // and cause the application to crash. int height = Resources.DisplayMetrics.HeightPixels; int width = _imageView.Height; //获取拍照的位图 App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height); if (App.bitmap != null) { //将图片绑定到控件上 _imageView.SetImageBitmap(App.bitmap); //清空bitmap 否则会出现oom问题 App.bitmap = null; } // Dispose of the Java side bitmap. GC.Collect(); } LoadAndResizeBitmap方法: public static Bitmap LoadAndResizeBitmap(this string fileName, int width, int height) { // First we get the the dimensions of the file on disk BitmapFactory.Options options = new BitmapFactory.Options { InJustDecodeBounds = true }; BitmapFactory.DecodeFile(fileName, options); // Next we calculate the ratio that we need to resize the image by // in order to fit the requested dimensions. int outHeight = options.OutHeight; int outWidth = options.OutWidth; int inSampleSize = 1; if (outHeight > height || outWidth > width) { inSampleSize = outWidth > outHeight ? outHeight / height : outWidth / width; } // Now we will load the image and have BitmapFactory resize it for us. options.InSampleSize = inSampleSize; options.InJustDecodeBounds = false; Bitmap resizedBitmap = BitmapFactory.DecodeFile(fileName, options); return resizedBitmap; } App类: public static class App { public static File _file; public static File _dir; public static Bitmap bitmap; } 最后再附上下载地址: 链接: https://pan.baidu.com/s/1h0Zg1jkCyKrZKN6N5eIB8Q 密码: wgdm

优秀的个人博客,低调大师

手机影音最终,软件退出功能的优化

现在软件到了主页面,点一次返回键后,就退出了,如果用户不小心点到了,就退出,比较不友好,那么就要在这里做一下判读,当用户2秒内再点击返回键后,就退出。 在MainActivity里,重写onKeyDown();方法。 定义一个boolean isExit=false; //判断是否退出操作 第一次接收到返回键监听时,将isExit设置为true,然后弹出toast,提示用户2秒内再按就退出。 然后延迟2秒发一个handler,在handler里将isExit=false;表示2秒内没按返回键,那么就需要再在2秒内点2次返回键才能退出 代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 boolean isExit; //是否退出 /** *实现2秒内按返回键,才退出的功能 *@paramkeyCode *@paramevent *@return */ @Override public boolean onKeyDown( int keyCode,KeyEventevent){ if (keyCode==KeyEvent.KEYCODE_BACK){ if (position!= 0 ){ //不是首页 position= 0 ; rg_bottom_tag.check(R.id.rb_video); return true ; } else if (!isExit){ isExit= true ; Toast.makeText(MainActivity. this , "2秒内再按一次退出" ,Toast.LENGTH_SHORT).show(); new Handler().postDelayed( new Runnable(){ @Override public void run(){ isExit= false ; } }, 2000 ); return true ; } } return super .onKeyDown(keyCode,event); } 由于视频只有讲到这里,这个项目就暂时到这里为止。如果后期能找到后面的视频,会把这个APP的后面功能加上去。 纪念我的第一个比较正式的Android项目,虽然没有完全做完功能,但是也是比较完善了。后面几天会总结下这个项目,然后继续努力学习。 作为一个从java转Android的人,我会继续努力去学习Android,积累技术实力。 技术改变命运!!! 2017年8月1日0:01分 于临海市 浙江省临海监狱招待所完成。 本文转自老婆的宝宝51CTO博客,原文链接:http://blog.51cto.com/cm0425/1952532,如需转载请自行联系原作者

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册