android 照个图 要开线程 不然永远拍的图都是那么的小_第1张图片

http://docs.xamarin.com/recipes/android/other_ux/camera_intent/take_a_picture_and_save_using_camera_app/

Main.axml.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <Button        android:id="@+id/myButton"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/openCamera" />    <ImageView        android:src="@android:drawable/ic_menu_gallery"        android:layout_width="fill_parent"        android:layout_height="300.0dp"        android:id="@+id/imageView1"        android:adjustViewBounds="true" /></LinearLayout>


Strings.xml<string name="openCamera">Open Camera</string>

定义几个全局变量

Java.IO.File _file;

Java.IO.File _dir;

ImageView _imageView;

入口处:

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;    }}

两个铺助方法 这个很重要 没有它就不成功了

private bool IsThereAnAppToTakePictures(){    Intent intent = new Intent(MediaStore.ActionImageCapture);    IList<ResolveInfo> availableActivities = PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);    return availableActivities != null && availableActivities.Count > 0;}private void CreateDirectoryForPictures(){    _dir = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "CameraAppDemo");    if (!_dir.Exists())    {        _dir.Mkdirs();    }}



事件开始

private void TakeAPicture(object sender, EventArgs eventArgs){    Intent intent = new Intent(MediaStore.ActionImageCapture);    _file = new File(_dir, String.Format("myPhoto_{0}.jpg", Guid.NewGuid()));    intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(_file));    StartActivityForResult(intent, 0);}

重载涵数

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data){    base.OnActivityResult(requestCode, resultCode, data);    // make it available in the gallery    Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);    Uri contentUri = Uri.FromFile(_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 = _imageView.Height;    int width = Resources.DisplayMetrics.WidthPixels;    using (Bitmap bitmap = _file.Path.LoadAndResizeBitmap(width, height))    {        _imageView.SetImageBitmap(bitmap);    }}


缩略图

public static class BitmapHelpers{    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;    }}




更多相关文章

  1. Android调用系统相机拍摄视频以及获取缩略图
  2. Android 获取最近应用的缩略图
  3. Android大图片之缩略图,以及对原图按照指定宽高裁剪成缩略图
  4. Java乔晓松-android中获取图片的缩略图(解决OutOfMemoryError)内
  5. 获取视频缩略图
  6. android获取图片和视频的缩略图
  7. Android 获取文件的缩略图

随机推荐

  1. android中AlertDialog 中setView用法的一
  2. Android之权限管理
  3. Android Touch事件传递机制解析
  4. java.lang.BootstrapMethodError: Except
  5. Android前端RxJava2+Retrofit2;后端Spring
  6. 【5年Android从零复盘系列之五】关于页面
  7. android fragment android.support.v4.ap
  8. Android:通过ValueAnimator动画改变控件长
  9. 关于 Android 进程保活,你所需要知道的一
  10. 【Android】Android Studio 使用第三方类