-
Notifications
You must be signed in to change notification settings - Fork 2
/
CameraActivity.java
81 lines (73 loc) · 2.46 KB
/
CameraActivity.java
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
package info.dourok.esactivity.sample.camera;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import info.dourok.esactivity.BuilderUtil;
import info.dourok.esactivity.sample.BuildConfig;
import info.dourok.esactivity.sample.R;
import java.io.File;
import java.io.IOException;
/** @author tiaolins */
public class CameraActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
findViewById(R.id.btn).setOnClickListener(v -> takePhoto());
}
private void takePhoto() {
Intent intentPhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ComponentName componentName = intentPhoto.resolveActivity(getPackageManager());
if (componentName != null) {
// 使用 lambda 表达式捕获局部变量,避免了将 tmpFile 作为类变量。
File tmpFile = getTempFile(FileType.IMG);
if (tmpFile != null) {
BuilderUtil.createBuilder(this, intentPhoto)
.asIntent()
.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
.putExtra(MediaStore.EXTRA_OUTPUT, getUri(componentName, tmpFile))
.asBuilder()
.forOk((context, intent) -> context.showPicture(tmpFile))
.start();
}
}
}
private void showPicture(File file) {
ImageView imageView = findViewById(R.id.photo);
imageView.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
}
private Uri getUri(ComponentName componentName, File file) {
Uri fileUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
grantUriPermission(
componentName.getPackageName(), fileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
return fileUri;
}
/**
* 创建临时文件
*
* @param type 文件类型
*/
public File getTempFile(FileType type) {
try {
File cacheDir = getFilesDir();
File file = File.createTempFile(type.toString(), null, cacheDir);
file.deleteOnExit();
return file;
} catch (IOException e) {
return null;
}
}
public enum FileType {
IMG,
AUDIO,
VIDEO,
FILE,
}
}