调用相机并将照片存储到sd卡上


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

用Intent启动相机的代码:

[java]  view plain  copy
 print ?
  1. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  2. startActivityForResult(intent, 1);  

拍完照后就可以在onActivityResult(int requestCode, int resultCode, Intent data)中获取到Bitmap对象了。

[java]  view plain  copy
 print ?
  1. Bitmap bitmap = (Bitmap) data.getExtras().get("data");  

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

[java]  view plain  copy
 print ?
  1. String sdStatus = Environment.getExternalStorageState();  
  2.         if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
  3.             Log.v("TestFile",  
  4.                     "SD card is not avaiable/writeable right now.");  
  5.             return;  
  6.         }  

以下代码可以实现将图像文件存到“sdcard/myImage/”文件夹下,名称为“111.jpg”

[java]  view plain  copy
 print ?
  1. File file = new File("/sdcard/myImage/");  
  2. file.mkdirs();// 创建文件夹  
  3. String fileName = "/sdcard/myImage/111.jpg";  
  4.   
  5. try {  
  6.     b = new FileOutputStream(fileName);  
  7.     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件  
  8. catch (FileNotFoundException e) {  
  9.     e.printStackTrace();  
  10. finally {  
  11.     try {  
  12.         b.flush();  
  13.         b.close();  
  14.     } catch (IOException e) {  
  15.         e.printStackTrace();  
  16.     }  
  17. }  
另外要注意的是读写sd卡文件必须首先要在Mainifest.xml文件中配置权限:

[html]  view plain  copy
 print ?
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  2. <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  


-----------------------------------------------------------------------------------------------------

一个demo,实现调用系统相机拍照,将其显示在屏幕上,并且存到sd卡。

完整代码如下:

MyCaremaActivity.java

[java]  view plain  copy
 print ?
  1. package barry.android.c;  
  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.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.util.Log;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.Button;  
  18. import android.widget.ImageView;  
  19.   
  20. public class MyCaremaActivity extends Activity {  
  21.   
  22.     @Override  
  23.     public void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.         setContentView(R.layout.main);  
  26.   
  27.         Button button = (Button) findViewById(R.id.button);  
  28.   
  29.         button.setOnClickListener(new OnClickListener() {  
  30.   
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  34.                 startActivityForResult(intent, 1);  
  35.             }  
  36.         });  
  37.     }  
  38.   
  39.     @Override  
  40.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  41.         super.onActivityResult(requestCode, resultCode, data);  
  42.   
  43.         if (resultCode == Activity.RESULT_OK) {  
  44.   
  45.             String sdStatus = Environment.getExternalStorageState();  
  46.             if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用  
  47.                 Log.v("TestFile",  
  48.                         "SD card is not avaiable/writeable right now.");  
  49.                 return;  
  50.             }  
  51.   
  52.             Bundle bundle = data.getExtras();  
  53.             Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式  
  54.             FileOutputStream b = null;  
  55.             File file = new File("/sdcard/myImage/");  
  56.             file.mkdirs();// 创建文件夹  
  57.             String fileName = "/sdcard/myImage/111.jpg";  
  58.   
  59.             try {  
  60.                 b = new FileOutputStream(fileName);  
  61.                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件  
  62.             } catch (FileNotFoundException e) {  
  63.                 e.printStackTrace();  
  64.             } finally {  
  65.                 try {  
  66.                     b.flush();  
  67.                     b.close();  
  68.                 } catch (IOException e) {  
  69.                     e.printStackTrace();  
  70.                 }  
  71.             }  
  72.   
  73.             ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);// 将图片显示在ImageView里  
  74.         }  
  75.     }  
  76. }  

main.xml
[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.   
  8.     <Button  
  9.         android:id="@+id/button"  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="点击启动相机" />  
  13.   
  14.     <ImageView  
  15.         android:id="@+id/imageView"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="fill_parent"  
  18.         android:background="#999999" />  
  19.   
  20. </LinearLayout>  

AndroidMainifest.xml

[html]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="barry.android.c"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="7" />  
  8.   
  9.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  10.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
  11.   
  12.     <application  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name" >  
  15.         <activity  
  16.             android:label="@string/app_name"  
  17.             android:name=".MyCaremaActivity" >  
  18.             <intent-filter >  
  19.                 <action android:name="android.intent.action.MAIN" />  
  20.   
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25.   
  26. </manifest>  

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

代码下载:http://download.csdn.net/detail/barryhappy/4138190

小米手机亲测可用。

----------------------------------------------------------------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值