首页 > 代码库 > Android系统拍照源码
Android系统拍照源码
个人对于Android系统拍照的一些总结:一种自定义图片拍照路径 ,另一种直接利用Android拍照后经过处理的缩略图
特别注意第一种方式需要增加SDK读写权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 下面直接上源码
1 package com.example.camerademo; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.IOException; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.graphics.Bitmap; 11 import android.graphics.BitmapFactory; 12 import android.net.Uri; 13 import android.os.Bundle; 14 import android.os.Environment; 15 import android.provider.MediaStore; 16 import android.view.View; 17 import android.widget.ImageView; 18 19 public class MainActivity extends Activity { 20 21 public static final int REQUEST_CAMERA_CODE = 100; 22 public static final int REQUEST_CAMERA_AUTO = 101; 23 ImageView mIv; 24 String path; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 mIv = (ImageView) findViewById(R.id.iv); 31 path = Environment.getExternalStorageDirectory() + "/" + "temp.jpg"; 32 } 33 34 public void doClick1(View v) { 35 Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); 36 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path))); 37 startActivityForResult(intent, REQUEST_CAMERA_CODE); 38 } 39 public void doClick2(View v) { 40 Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE); 41 startActivityForResult(intent, REQUEST_CAMERA_AUTO); 42 } 43 44 45 46 @Override 47 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 48 super.onActivityResult(requestCode, resultCode, data); 49 if(resultCode == RESULT_OK) { 50 if(requestCode == REQUEST_CAMERA_CODE ) { 51 FileInputStream fis = null; 52 try { 53 fis = new FileInputStream(path); 54 Bitmap bitmap = BitmapFactory.decodeStream(fis); 55 if(bitmap != null) { 56 mIv.setImageBitmap(bitmap); 57 } 58 } catch (FileNotFoundException e) { 59 // TODO Auto-generated catch block 60 e.printStackTrace(); 61 } finally{ 62 if(fis != null) { 63 try { 64 fis.close(); 65 } catch (IOException e) { 66 // TODO Auto-generated catch block 67 e.printStackTrace(); 68 } 69 } 70 } 71 72 } else if(requestCode == REQUEST_CAMERA_AUTO) { 73 Bundle bundle = data.getExtras(); 74 Bitmap bitmap = (Bitmap) bundle.get("data"); 75 if(bitmap != null) { 76 mIv.setImageBitmap(bitmap); 77 } 78 } 79 } 80 81 } 82 83 }
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 tools:context="${relativePackage}.${activityClass}" > 6 7 <TextView 8 android:id="@+id/textView1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="@string/hello_world" /> 12 13 <Button 14 android:id="@+id/startCamera" 15 android:onClick="doClick1" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content" 18 android:layout_below="@+id/textView1" 19 android:layout_centerHorizontal="true" 20 android:layout_marginTop="17dp" 21 android:text="startCamera_custome_address" /> 22 23 <ImageView 24 android:id="@+id/iv" 25 android:layout_width="wrap_content" 26 android:layout_height="wrap_content" 27 android:layout_alignLeft="@+id/startCamera" 28 android:layout_centerVertical="true" 29 android:layout_marginLeft="32dp" /> 30 31 <Button 32 android:id="@+id/button1" 33 android:onClick="doClick2" 34 android:layout_width="wrap_content" 35 android:layout_height="wrap_content" 36 android:layout_alignLeft="@+id/startCamera" 37 android:layout_below="@+id/startCamera" 38 android:layout_marginTop="41dp" 39 android:text="start_camera_auto_addres" /> 40 41 </RelativeLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.camerademo" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="8" 9 android:targetSdkVersion="21" /> 10 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 11 12 <application 13 android:allowBackup="true" 14 android:icon="@drawable/ic_launcher" 15 android:label="@string/app_name" 16 android:theme="@style/AppTheme" > 17 <activity 18 android:name=".MainActivity" 19 android:label="@string/app_name" > 20 <intent-filter> 21 <action android:name="android.intent.action.MAIN" /> 22 23 <category android:name="android.intent.category.LAUNCHER" /> 24 </intent-filter> 25 </activity> 26 </application> 27 28 </manifest>
Android系统拍照源码
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。