最近開發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