android调用系统相机拍照并保存在本地

本文介绍了一个简单的Android应用实现,该应用通过调用相机拍摄照片,并将其保存到SD卡上。包括权限申请、文件操作以及使用Intent进行相机调用的基本流程。
[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.io.File;  
  2. import java.io.FileNotFoundException;  
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.util.Calendar;  
  6. import java.util.Locale;  
  7. import android.annotation.SuppressLint;  
  8. import android.app.Activity;  
  9. import android.content.Intent;  
  10. import android.graphics.Bitmap;  
  11. import android.os.Bundle;  
  12. import android.os.Environment;  
  13. import android.provider.MediaStore;  
  14. import android.text.format.DateFormat;  
  15. import android.util.Log;  
  16. import android.view.View;  
  17. import android.view.View.OnClickListener;  
  18. import android.widget.Button;  
  19. import android.widget.ImageView;  
  20. import android.widget.Toast;  
  21.   
  22. @SuppressLint("SdCardPath")  
  23. public class MainActivity extends Activity {  
  24.     /** Called when the activity is first created. */  
  25.     private Button button;  
  26.     private ImageView view;  
  27.   
  28.     @Override  
  29.     public void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.activity_main);  
  32.         button = (Button) findViewById(R.id.button1);  
  33.         view= (ImageView)findViewById(R.id.imageView1);  
  34.         button.setOnClickListener(new OnClickListener() {  
  35.   
  36.             @Override  
  37.             public void onClick(View v) {  
  38.                 // TODO Auto-generated method stub  
  39.                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  40.                 startActivityForResult(intent, 1);  
  41.             }  
  42.         });  
  43.     }  
  44.   
  45.     @SuppressLint("SdCardPath")  
  46.     @Override  
  47.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  48.         // TODO Auto-generated method stub  
  49.         super.onActivityResult(requestCode, resultCode, data);  
  50.         if (resultCode == Activity.RESULT_OK) {  
  51.             String sdStatus = Environment.getExternalStorageState();  
  52.             if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
  53.                 Log.i("TestFile",  
  54.                         "SD card is not avaiable/writeable right now.");  
  55.                 return;  
  56.             }  
  57.             new DateFormat();  
  58.             String name = DateFormat.format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";   
  59.             Toast.makeText(this, name, Toast.LENGTH_LONG).show();  
  60.             Bundle bundle = data.getExtras();  
  61.             Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式  
  62.           
  63.             FileOutputStream b = null;           
  64.             File file = new File("/sdcard/Image/");  
  65.             file.mkdirs();// 创建文件夹  
  66.             String fileName = "/sdcard/Image/"+name;  
  67.   
  68.             try {  
  69.                 b = new FileOutputStream(fileName);  
  70.                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件  
  71.             } catch (FileNotFoundException e) {  
  72.                 e.printStackTrace();  
  73.             } finally {  
  74.                 try {  
  75.                     b.flush();  
  76.                     b.close();  
  77.                 } catch (IOException e) {  
  78.                     e.printStackTrace();  
  79.                 }  
  80.             }  
  81.             try  
  82.             {  
  83.                 view.setImageBitmap(bitmap);// 将图片显示在ImageView里  
  84.             }catch(Exception e)  
  85.             {  
  86.                 Log.e("error", e.getMessage());  
  87.             }  
  88.               
  89.         }  
  90.     }  
  91. }  

xml文件

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingBottom="@dimen/activity_vertical_margin"  
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  7.     android:paddingRight="@dimen/activity_horizontal_margin"  
  8.     android:paddingTop="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity" >  
  10.   
  11.     <ImageView  
  12.         android:id="@+id/imageView1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:src="@drawable/ic_launcher" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/button1"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignParentBottom="true"  
  23.         android:layout_centerHorizontal="true"  
  24.         android:layout_marginBottom="15dp"  
  25.         android:text="Button" />  
  26.   
  27. </RelativeLayout>  

最后要记得加权限

<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
  <uses-feature android:name = "android.hardware.camera" />
<uses-feature android:name = "android.hardware.camera.autofocus" />


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值