android 照相获取图片路径

在android中,照相功能系统已经提供,在app中可以直接使用。当手机从android play里面下载有照相功能的应用时, 会判断手机是否支持。不支持,不给予下载。

照相有几个步骤:
1. 声明权限
2. 使用Camera照相
3. 显示图片

1. 声明权限

在manifest里面声明使用Camera:
[html]  view plain copy
  1. <uses-feature android:name="android.hardware.camera" />  

2. 使用Camera照相
在Activity中,调用Camera应用
[java]  view plain copy
  1. private void dispatchTakePictureIntent(int actionCode) {  
  2.     Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  3.     startActivityForResult(takePictureIntent, actionCode);  
  4. }  

3. 显示图片
在使用Camera照相成功之后,会返回回来,要显示图片就必须先获取图片,然后显示出来。
在onActivityResult方法中取得
[java]  view plain copy
  1. <pre name="code" class="java">@Override  
  2.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  3.         switch (requestCode) {  
  4.         Bundle extras = intent.getExtras();  
  5.         Bitmap mImageBitmap = (Bitmap) extras.get("data");  
  6.         mImageView.setImageBitmap(mImageBitmap);  
  7. }</pre>  
  8. <pre></pre>  
  9. <pre></pre>  


想要保存图片到制定目录,启动Camera应用时,需要指定文件

[java]  view plain copy
  1.   
[java]  view plain copy
  1. Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  2. File f = null;  
  3.               
  4.             try {  
  5.                 f = setUpPhotoFile();   
  6.                 takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));  
  7.             } catch (IOException e) {  
  8.                 e.printStackTrace();  
  9.                 f = null;  
  10.             }  
[java]  view plain copy
  1.   
[java]  view plain copy
  1. private File createImageFile() throws IOException {  
  2.         // Create an image file name  
  3.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());  
  4.         String imageFileName = "IMG_"+ timeStamp + "_";  
  5.         File albumF = getAlbumDir();  
  6.         File imageF = File.createTempFile(imageFileName, "jpg", albumF);  
  7.         return imageF;  
  8.     }  
  9.   
  10.   
  11.     private File setUpPhotoFile() throws IOException {  
  12.           
  13.         File f = createImageFile();  
  14.         mCurrentPhotoPath = f.getAbsolutePath();  
  15.           
  16.         return f;  
  17.     }  
  18. private File getAlbumDir() {  
  19.         File storageDir = null;  
  20.   
  21.   
  22.         if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {  
  23.               
  24.             storageDir = mAlbumStorageDirFactory.getAlbumStorageDir(getAlbumName());  
  25.   
  26.   
  27.             if (storageDir != null) {  
  28.                 if (! storageDir.mkdirs()) {  
  29.                     if (! storageDir.exists()){  
  30.                         Log.d("CameraSample""failed to create directory");  
  31.                         return null;  
  32.                     }  
  33.                 }  
  34.             }  
  35.               
  36.         } else {  
  37.             Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");  
  38.         }  
  39.           
  40.         return storageDir;  
  41.     }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值