zoukankan      html  css  js  c++  java
  • [转]调用相机并将照片存储到sd卡上

    本文转自:http://blog.csdn.net/barryhappy/article/details/7355317

    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.   
    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文件中配置权限:

    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.   
    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

    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

    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

  • 相关阅读:
    迭代器和生成器
    装饰器进阶
    闭包和装饰器
    函数的嵌套
    函数的参数
    什么是粘包现象
    模拟ssh远程命令执行
    客户端与服务端代码bug修复和加入循环
    用socket实现简单的套接字通讯
    网络编程之客户端和服务端,
  • 原文地址:https://www.cnblogs.com/freeliver54/p/2495873.html
Copyright © 2011-2022 走看看