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

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

发表于 2012-3-16 08:46:31 |查看: 3541 |回复: 128

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

用Intent启动相机的代码:

  1. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  2. startActivityForResult(intent, 1);
复制代码
拍完照后就可以在onActivityResult(int requestCode, int resultCode, Intent data)中获取到Bitmap对象了。
  1. Bitmap bitmap = (Bitmap) data.getExtras().get("data");
复制代码
要将图像存储到sd卡之前最好先检查一下sd卡是否可用
  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”
  1. File file = new File("/sdcard/myImage/");
  2.             file.mkdirs();// 创建文件夹
  3.             String fileName = "/sdcard/myImage/111.jpg";

  4.             try {
  5.                 b = new FileOutputStream(fileName);
  6.                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
  7.             } catch (FileNotFoundException e) {
  8.                 e.printStackTrace();
  9.             } finally {
  10.                 try {
  11.                     b.flush();
  12.                     b.close();
  13.                 } catch (IOException e) {
  14.                     e.printStackTrace();
  15.                 }
  16.             }
复制代码
另外要注意的是读写sd卡文件必须首先要在Mainifest.xml文件中配置权限:
  1. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  2.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
复制代码
一个demo,实现调用系统相机拍照,将其显示在屏幕上,并且存到sd卡。完整代码如下:MyCaremaActivity.java
  1. package barry.android.c;

  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;

  6. import android.app.Activity;
  7. import android.content.Intent;
  8. import android.graphics.Bitmap;
  9. import android.os.Bundle;
  10. import android.os.Environment;
  11. import android.provider.MediaStore;
  12. import android.util.Log;
  13. import android.view.View;
  14. import android.view.View.OnClickListener;
  15. import android.widget.Button;
  16. import android.widget.ImageView;

  17. public class MyCaremaActivity extends Activity {

  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState) {
  20.         super.onCreate(savedInstanceState);
  21.         setContentView(R.layout.main);

  22.         Button button = (Button) findViewById(R.id.button);

  23.         button.setOnClickListener(new OnClickListener() {

  24.             @Override
  25.             public void onClick(View v) {
  26.                 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  27.                 startActivityForResult(intent, 1);
  28.             }
  29.         });
  30.     }

  31.     @Override
  32.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  33.         super.onActivityResult(requestCode, resultCode, data);

  34.         if (resultCode == Activity.RESULT_OK) {

  35.             String sdStatus = Environment.getExternalStorageState();
  36.             if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
  37.                 Log.v("TestFile",
  38.                         "SD card is not avaiable/writeable right now.");
  39.                 return;
  40.             }

  41.             Bundle bundle = data.getExtras();
  42.             Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
  43.             FileOutputStream b = null;
  44.             File file = new File("/sdcard/myImage/");
  45.             file.mkdirs();// 创建文件夹
  46.             String fileName = "/sdcard/myImage/111.jpg";

  47.             try {
  48.                 b = new FileOutputStream(fileName);
  49.                 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
  50.             } catch (FileNotFoundException e) {
  51.                 e.printStackTrace();
  52.             } finally {
  53.                 try {
  54.                     b.flush();
  55.                     b.close();
  56.                 } catch (IOException e) {
  57.                     e.printStackTrace();
  58.                 }
  59.             }

  60.             ((ImageView) findViewById(R.id.imageView)).setImageBitmap(bitmap);// 将图片显示在ImageView里
  61.         }
  62.     }
  63. }
复制代码
main.xml
  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.     <Button
  7.         android:id="@+id/button"
  8.         android:layout_width="fill_parent"
  9.         android:layout_height="wrap_content"
  10.         android:text="点击启动相机" />

  11.     <ImageView
  12.         android:id="@+id/imageView"
  13.         android:layout_width="fill_parent"
  14.         android:layout_height="fill_parent"
  15.         android:background="#999999" />

  16. </LinearLayout>
复制代码
AndroidMainifest.xml
  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.     <uses-sdk android:minSdkVersion="7" />

  7.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  8.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

  9.     <application
  10.         android:icon="@drawable/ic_launcher"
  11.         android:label="@string/app_name" >
  12.         <activity
  13.             android:label="@string/app_name"
  14.             android:name=".MyCaremaActivity" >
  15.             <intent-filter >
  16.                 <action android:name="android.intent.action.MAIN" />

  17.                 <category android:name="android.intent.category.LAUNCHER" />
  18.             </intent-filter>
  19.         </activity>
  20.     </application>

  21. </manifest>
复制代码


回复即可下载源码哦:
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值