Android系统拍照源码

时间:2022-02-11 18:10:14

个人对于Android系统拍照的一些总结:一种自定义图片拍照路径 ,另一种直接利用Android拍照后经过处理的缩略图

特别注意第一种方式需要增加SDK读写权限: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 下面直接上源码

 package com.example.camerademo;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException; import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends Activity { public static final int REQUEST_CAMERA_CODE = 100;
public static final int REQUEST_CAMERA_AUTO = 101;
ImageView mIv;
String path; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mIv = (ImageView) findViewById(R.id.iv);
path = Environment.getExternalStorageDirectory() + "/" + "temp.jpg";
} public void doClick1(View v) {
Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
startActivityForResult(intent, REQUEST_CAMERA_CODE);
}
public void doClick2(View v) {
Intent intent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA_AUTO);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
if(requestCode == REQUEST_CAMERA_CODE ) {
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
Bitmap bitmap = BitmapFactory.decodeStream(fis);
if(bitmap != null) {
mIv.setImageBitmap(bitmap);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} } else if(requestCode == REQUEST_CAMERA_AUTO) {
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
if(bitmap != null) {
mIv.setImageBitmap(bitmap);
}
}
} } }
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <Button
android:id="@+id/startCamera"
android:onClick="doClick1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="startCamera_custome_address" /> <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startCamera"
android:layout_centerVertical="true"
android:layout_marginLeft="32dp" /> <Button
android:id="@+id/button1"
android:onClick="doClick2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startCamera"
android:layout_below="@+id/startCamera"
android:layout_marginTop="41dp"
android:text="start_camera_auto_addres" /> </RelativeLayout>
 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.camerademo"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>