相册服务
- 判断是否有外存
- 通过startActivityForResult(intent, MainActivity.GALLERY_REQUET)返回主Activity获得result.
- 重写方法 protected void onActivityResult(int requestCode, int resultCode, Intent data);
1 | /** |
相机服务
这里给出的就是拍照并根据时间保存相片,然后image_path作为全局变量,在onActivityResult(int requestCode, int resultCode, Intent data);继续使用
1 | /** |
onActivityResult
在Activity跳转中很常用
这里用于系统服务和我们程序跳转,我想原理应该类似
onActivityResult(int requestCode, int resultCode, Intent data)不多说,生命周期
WARNING:
- CAMERA_REQUEST中是通过image_path生成File再生成的Uri,没有使用Intent data中的数据
- 在Fragment中调用MainActivity时候要强转为Activity类,即((MainActivity)getActivity()).takeCamera();
这两点我在做的时候,疑惑了好久!后来发现,不难理解
源代码
MainActivity.java1
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//全局变量
private Bitmap photo;
//系统参数
private String image_path;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAMERA_REQUEST && resultCode == RESULT_OK){
File file=new File(image_path);
try {
Uri uri = Uri.fromFile(file);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
options.inSampleSize=4;
options.inJustDecodeBounds=false;
Bitmap map=BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), map, null, null);
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,uri));
// image.setImageBitmap(map);
photo = map;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
if(requestCode == GALLERY_REQUET && resultCode == RESULT_OK){
ContentResolver resolver = getContentResolver();
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = resolver.query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String choosePicturePath = cursor.getString(columnIndex);
cursor.close();
Log.i("Target",choosePicturePath);
photo = BitmapFactory.decodeFile(choosePicturePath);
}
}
//========================华丽丽的分割线============================
//=========================调用系统服务============================
/**
* 调用相机服务
*/
public void takeCamera(){
String path = Environment.getExternalStorageDirectory()+File.separator+Environment.DIRECTORY_DCIM;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intent.resolveActivity(getPackageManager()) != null ){
String temName = new DateFormat().format("yyMMdd_hhmmss",System.currentTimeMillis()) + "_" + (Math.random()*100)+".jpg";
image_path = path + File.separator + temName;
File file=new File(image_path);
if(file.exists()){
file.delete();
}
Uri uri=Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, CAMERA_REQUEST);
Log.i("Target", "照相");
}
}
/**
* 调用相册服务
*/
public void takePhoto(){
Intent intent = null;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
intent=new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra("cameraPhoto", true);
startActivityForResult(intent, MainActivity.GALLERY_REQUET);
}
else {
Toast.makeText(this, "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
}
Log.i("Target", "选取照片");
}