现在的位置: 首页 > 综合 > 正文

Android实现拍照功能。

2014年01月03日 ⁄ 综合 ⁄ 共 5412字 ⁄ 字号 评论关闭

Android中实现拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据。 还有一种是自己用Camera类和其他相关类实现相机功能,这种方法定制度比较高,比较复杂,一般平常的应用只需使用第一种即可。

用Intent启动相机的代码:

Java代码  收藏代码
  1. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  2. startActivityForResult(intent, 1);  

 要将图像存储到SD卡上之前,最好检查一下SD卡是否可用

 

Java代码  收藏代码
  1. String sdStatus = Environment.getExternalStorageState();  
  2.             if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
  3.                 Log.v("TestFile",  
  4.                         "SD card is not available/writeable right now.");  
  5.                 return;  
  6.             }  

另外,要注意的是读写SD卡必须在Mainifest.xml文件中配置权限: 

Java代码  收藏代码
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  

 一个示例如下:

 

MainActivity.java:

 

 

 

Java代码  收藏代码
  1. package com.example.testand;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileNotFoundException;  
  5. import java.io.FileOutputStream;  
  6. import java.io.IOException;  
  7.   
  8. import android.os.Bundle;  
  9. import android.os.Environment;  
  10. import android.provider.MediaStore;  
  11. import android.app.Activity;  
  12. import android.content.Intent;  
  13. import android.graphics.Bitmap;  
  14. import android.util.Log;  
  15. import android.view.Menu;  
  16. import android.view.View;  
  17. import android.view.View.OnClickListener;  
  18. import android.widget.Button;  
  19. import android.widget.ImageView;  
  20.   
  21. public class MainActivity extends Activity {  
  22.   
  23.     @Override  
  24.     protected void onCreate(Bundle savedInstanceState) {  
  25.         super.onCreate(savedInstanceState);  
  26.         setContentView(R.layout.activity_main);  
  27.           
  28.         Button button=(Button) findViewById(R.id.button);  
  29.           
  30.         button.setOnClickListener(new OnClickListener(){  
  31.   
  32.             @Override  
  33.             public void onClick(View v) {  
  34.                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  35.                 startActivityForResult(intent, 1);  
  36.                   
  37.             }  
  38.               
  39.         });  
  40.           
  41.           
  42.           
  43.     }  
  44.   
  45.     protected void onActivityResult(int requestCode, int resultCode, Intent data){  
  46.         super.onActivityResult(requestCode, resultCode, data);  
  47.           
  48.         String sdStatus = Environment.getExternalStorageState();  
  49.         if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
  50.             Log.v("TestFile","SD card is not avaiable/writeable right now.");  
  51.             return;  
  52.         }  
  53.         Bundle bundle = data.getExtras();  
  54.         Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式  
  55.         File file = new File("/sdcard/myImage/");  
  56.         FileOutputStream bout = null;  
  57.           
  58.         file.mkdirs();// 创建文件夹  
  59.         String fileName = "/sdcard/myImage/111.jpg";  
  60.   
  61.         try {  
  62.             bout = new FileOutputStream(fileName);  
  63.             bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bout);// 把数据写入文件  
  64.         } catch (FileNotFoundException e) {  
  65.             e.printStackTrace();  
  66.         } finally {  
  67.             try {  
  68.                 bout.flush();  
  69.                 bout.close();  
  70.             } catch (IOException e) {  
  71.                 e.printStackTrace();  
  72.             }  
  73.         }  
  74.         ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);// 将图片显示在ImageView里  
  75.     }  
  76.       
  77.       
  78.     @Override  
  79.     public boolean onCreateOptionsMenu(Menu menu) {  
  80.         // Inflate the menu; this adds items to the action bar if it is present.  
  81.         getMenuInflater().inflate(R.menu.main, menu);  
  82.         return true;  
  83.     }  
  84.   
  85. }  

 

activity_main.xml:

Xml代码  收藏代码
  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.      
  12.   
  13.     <Button  
  14.         android:id="@+id/button"  
  15.         android:layout_width="fill_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="点击启动相机" />  
  18.   
  19.     <ImageView  
  20.         android:id="@+id/imageView"  
  21.         android:layout_width="fill_parent"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_alignLeft="@+id/button"  
  24.         android:layout_alignParentBottom="true"  
  25.         android:layout_below="@+id/button"  
  26.         android:background="#999999" />  
  27.       
  28. </RelativeLayout>  

 AndroidManifest.xml:

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.testand"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="18" />  
  10.   
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.example.testand.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.   
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.           
【上篇】
【下篇】

抱歉!评论已关闭.