forked from D-clock/AndroidStudyCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapturePhotoHelper.java
More file actions
128 lines (110 loc) · 3.47 KB
/
CapturePhotoHelper.java
File metadata and controls
128 lines (110 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.clock.study.helper;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.provider.MediaStore;
import android.widget.Toast;
import com.clock.study.R;
import com.clock.utils.bitmap.BitmapUtils;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
/**
* 拍照辅助类
* <p/>
* Created by Clock on 2016/5/21.
*/
public class CapturePhotoHelper {
private final static String TIMESTAMP_FORMAT = "yyyy_MM_dd_HH_mm_ss";
public final static int CAPTURE_PHOTO_REQUEST_CODE = 0x1111;
private Activity mActivity;
/**
* 存放图片的目录
*/
private File mPhotoFolder;
/**
* 拍照生成的图片文件
*/
private File mPhotoFile;
/**
* @param activity
* @param photoFolder 存放生成照片的目录,目录不存在时候会自动创建,但不允许为null;
*/
public CapturePhotoHelper(Activity activity, File photoFolder) {
this.mActivity = activity;
this.mPhotoFolder = photoFolder;
}
/**
* 拍照
*/
public void capture() {
if (hasCamera()) {
createPhotoFile();
if (mPhotoFile == null) {
Toast.makeText(mActivity, R.string.camera_open_error, Toast.LENGTH_SHORT).show();
return;
}
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = Uri.fromFile(mPhotoFile);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
mActivity.startActivityForResult(captureIntent, CAPTURE_PHOTO_REQUEST_CODE);
} else {
Toast.makeText(mActivity, R.string.camera_open_error, Toast.LENGTH_SHORT).show();
}
}
/**
* 创建照片文件
*/
private void createPhotoFile() {
if (mPhotoFolder != null) {
if (!mPhotoFolder.exists()) {//检查保存图片的目录存不存在
mPhotoFolder.mkdirs();
}
String fileName = new SimpleDateFormat(TIMESTAMP_FORMAT).format(new Date());
mPhotoFile = new File(mPhotoFolder, fileName + BitmapUtils.JPG_SUFFIX);
if (mPhotoFile.exists()) {
mPhotoFile.delete();
}
try {
mPhotoFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
mPhotoFile = null;
}
} else {
mPhotoFile = null;
Toast.makeText(mActivity, R.string.not_specify_a_directory, Toast.LENGTH_SHORT).show();
}
}
/**
* 判断系统中是否存在可以启动的相机应用
*
* @return 存在返回true,不存在返回false
*/
public boolean hasCamera() {
PackageManager packageManager = mActivity.getPackageManager();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
/**
* 获取当前拍到的图片文件
*
* @return
*/
public File getPhoto() {
return mPhotoFile;
}
/**
* 设置照片文件
*
* @param photoFile
*/
public void setPhoto(File photoFile) {
this.mPhotoFile = photoFile;
}
}