android 照相
16lz
2022-07-03
android 照相
在android中,照相功能系统已经提供,在app中可以直接使用。当手机从android play里面下载有照相功能的应用时, 会判断手机是否支持。不支持,不给予下载。 照相有几个步骤: 1. 声明权限 2. 使用Camera照相 3. 显示图片1. 声明权限
在manifest里面声明使用Camera:<uses-feature android:name="android.hardware.camera" />
2. 使用Camera照相
在Activity中,调用Camera应用
private void dispatchTakePictureIntent(int actionCode) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(takePictureIntent, actionCode);}
3. 显示图片
在使用Camera照相成功之后,会返回回来,要显示图片就必须先获取图片,然后显示出来。 在onActivityResult方法中取得
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {Bundle extras = intent.getExtras();Bitmap mImageBitmap = (Bitmap) extras.get("data");mImageView.setImageBitmap(mImageBitmap);}
想要保存图片到制定目录,启动Camera应用时,需要指定文件
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);File f = null;try {f = setUpPhotoFile(); takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));} catch (IOException e) {e.printStackTrace();f = null;}
private File createImageFile() throws IOException {// Create an image file nameString timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());String imageFileName = "IMG_"+ timeStamp + "_";File albumF = getAlbumDir();File imageF = File.createTempFile(imageFileName, "jpg", albumF);return imageF;}private File setUpPhotoFile() throws IOException {File f = createImageFile();mCurrentPhotoPath = f.getAbsolutePath();return f;}private File getAlbumDir() {File storageDir = null;if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {storageDir = mAlbumStorageDirFactory.getAlbumStorageDir(getAlbumName());if (storageDir != null) {if (! storageDir.mkdirs()) {if (! storageDir.exists()){Log.d("CameraSample", "failed to create directory");return null;}}}} else {Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");}return storageDir;}
更多相关文章
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- android 使用html5作布局文件: webview跟javascript交互
- Android(安卓)matrix 控制图片的旋转、缩放、移动
- Android(安卓)Resource介绍和使用
- "Failed to fetch URL https://dl-ssl.google.com/android/repos
- 使用NetBeans搭建Android开发环境