-
Notifications
You must be signed in to change notification settings - Fork 1
/
CaptureImageOperation1
72 lines (63 loc) · 3.72 KB
/
CaptureImageOperation1
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
Final Step part 1
btnAddImg.setOnClickListener(new View.OnClickListener() {// button to get the image
@Override
public void onClick(View view) {
selectImage();
}
});
// Select image from camera and gallery
private void selectImage() {
try {
PackageManager pm = getActivity().getPackageManager();
int hasPerm = pm.checkPermission(Manifest.permission.CAMERA, getActivity().getPackageName());
if (hasPerm == PackageManager.PERMISSION_GRANTED) {
final CharSequence[] options = {"Take Photo", "Choose From Gallery","Cancel"};
android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder(getActivity());
builder.setTitle("Select Option");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
dialog.dismiss();
//https://developer.android.com/training/camera/photobasics from here i got the answer
// and here
//https://github.com/prudhvirajkumar10/imagepickerdemo
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//this code access camera
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
}
catch (IOException e) {
Toast.makeText(getActivity(), "error creating file", Toast.LENGTH_SHORT).show(); e.printStackTrace();
return;
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoUri = FileProvider.getUriForFile(getActivity(), "com.example.android.fileprovider", photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
if (photoUri==null){
Toast.makeText(getActivity(), "photo uri null", Toast.LENGTH_SHORT).show();
}
startActivityForResult(intent, PICK_IMAGE_CAMERA);
}
}
} else if (options[item].equals("Choose From Gallery")) {
dialog.dismiss();
Intent pickPhoto = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (pickPhoto.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(pickPhoto, PICK_IMAGE_GALLERY);
}
} else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
} else
Toast.makeText(getActivity(), "Camera Permission error", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getActivity(), "Camera Permission error", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}//end catch
}//end selectimage()