1 | package org.tipple.mapDisplay;
|
---|
2 |
|
---|
3 | import java.io.File;
|
---|
4 | import java.io.FileOutputStream;
|
---|
5 | import java.io.ObjectOutputStream;
|
---|
6 |
|
---|
7 | import android.app.Activity;
|
---|
8 | import android.content.ContentResolver;
|
---|
9 | import android.content.Context;
|
---|
10 | import android.content.Intent;
|
---|
11 | import android.graphics.Bitmap;
|
---|
12 | import android.net.Uri;
|
---|
13 | import android.os.Bundle;
|
---|
14 | import android.os.Environment;
|
---|
15 | import android.provider.MediaStore;
|
---|
16 | import android.util.Log;
|
---|
17 |
|
---|
18 | public class TakePicture extends Activity {
|
---|
19 | protected Uri imageUri;
|
---|
20 | String filename = "imageExample.jpg";
|
---|
21 | File photo;
|
---|
22 |
|
---|
23 | @Override
|
---|
24 | public void onCreate(Bundle savedInstanceState) {
|
---|
25 | super.onCreate(savedInstanceState);
|
---|
26 |
|
---|
27 | // retrieve value from picture taking
|
---|
28 | Bundle extras = getIntent().getExtras();
|
---|
29 | if (extras != null) {
|
---|
30 | filename = extras.getString("filename");
|
---|
31 | }
|
---|
32 | startCameraActivity(filename);
|
---|
33 |
|
---|
34 | }
|
---|
35 |
|
---|
36 | protected void startCameraActivity(String filename) {
|
---|
37 |
|
---|
38 | Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
|
---|
39 | photo = new File(Environment.getExternalStorageDirectory(), filename);
|
---|
40 | intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
|
---|
41 | imageUri = Uri.fromFile(photo);
|
---|
42 | startActivityForResult(intent, 0);
|
---|
43 |
|
---|
44 | }
|
---|
45 |
|
---|
46 | public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
---|
47 |
|
---|
48 | super.onActivityResult(requestCode, resultCode, data);
|
---|
49 | Log.i("TakePicture", "resultCode: " + resultCode);
|
---|
50 | switch (resultCode) {
|
---|
51 | case 0:
|
---|
52 | Log.i("TakePicture", "User cancelled");
|
---|
53 | break;
|
---|
54 |
|
---|
55 | case -1:
|
---|
56 | onPhotoTaken();
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | this.finish(); // when user click on V then back to previous activity
|
---|
60 |
|
---|
61 | }
|
---|
62 |
|
---|
63 | private void onPhotoTaken() {
|
---|
64 | Uri selectedImage = imageUri;
|
---|
65 | getContentResolver().notifyChange(selectedImage, null);
|
---|
66 | ContentResolver cr = getContentResolver();
|
---|
67 |
|
---|
68 | try {
|
---|
69 | Bitmap bitmap = android.provider.MediaStore.Images.Media.getBitmap(
|
---|
70 | cr, selectedImage);
|
---|
71 | Log.i("TakePicture", "save file to the location" + filename);
|
---|
72 | savePicture(tippleActivity.imageDirectory + filename, bitmap,
|
---|
73 | getApplicationContext());
|
---|
74 | } catch (Exception e) {
|
---|
75 | e.printStackTrace();
|
---|
76 | }
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void savePicture(String filename, Bitmap b, Context ctx) {
|
---|
80 | if (!photo.exists()) {// look for sdcard directory
|
---|
81 | try {
|
---|
82 | ObjectOutputStream oos;
|
---|
83 | FileOutputStream out = new FileOutputStream(filename);
|
---|
84 | out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
|
---|
85 | oos = new ObjectOutputStream(out);
|
---|
86 | b.compress(Bitmap.CompressFormat.PNG, 100, oos);
|
---|
87 | oos.close();
|
---|
88 | oos.notifyAll();
|
---|
89 | out.notifyAll();
|
---|
90 | out.close();
|
---|
91 | } catch (Exception e) {
|
---|
92 | e.printStackTrace();
|
---|
93 | }
|
---|
94 | } else {
|
---|
95 | Log.i("TakePicture", "file exist" + filename);
|
---|
96 | return;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | }
|
---|