-
Notifications
You must be signed in to change notification settings - Fork 2
/
Write.java
162 lines (131 loc) · 5.19 KB
/
Write.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package com.example.travel;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager.Request;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.SyncStateContract.Constants;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class Write extends Activity {
private static final int REQUEST_CODE_CAPTURE_CAMEIA = 0;
private static final int REQUEST_CODE_PICK_IMAGE = 1;
private String savepath = "pic";
private ImageButton bt_pic=null;
private Button bt_OK=null;
private Button bt_cancer=null;
private String capturePath = null;
private int traid;
private File savedir=null;
private Bitmap photo=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.write);
Bundle extras = getIntent().getExtras();
traid = extras.getInt("traid");
savedir = new File(traid+"");
if (!savedir.exists()) {savedir.mkdirs();}
bt_pic=(ImageButton) findViewById(R.id.write_pic);
bt_pic.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
new AlertDialog.Builder(Write.this).setTitle("操作").setItems(
new String[] { "来自相册","拍照" },
new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog,int which) {
// Toast.makeText( Write.this, "You choose " +which, Toast.LENGTH_LONG).show();
if (which==0){
getImageFromAlbum();
}else {
getImageFromCamera();
}
}
}).setNegativeButton("取消", null).show();
}
});
bt_OK=(Button)findViewById(R.id.write_OK);
bt_OK.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText( Write.this, "you click the OK button", Toast.LENGTH_LONG).show();
//String spath=savedir.toString()+"/hello_file.jpg";
//saveImage(photo, spath);
Log.e( "aaaaaaaaa",savedir.toString()+" "+savedir.getPath());
}});
bt_cancer=(Button)findViewById(R.id.write_cancer);
bt_cancer.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Toast.makeText( Write.this, "you click the CANCER button", Toast.LENGTH_LONG).show();
}});
}
protected void getImageFromAlbum() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_PICK_IMAGE);
}
protected void getImageFromCamera() {
String state = Environment.getExternalStorageState();
Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri uri = data.getData();
ContentResolver resolver = getContentResolver();
if (requestCode == REQUEST_CODE_PICK_IMAGE) {
try {
photo = MediaStore.Images.Media.getBitmap(resolver, uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {
//use bundle to get data
Bundle bundle = data.getExtras();
if (bundle != null) {
photo = (Bitmap) bundle.get("data");
} else {
Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_LONG).show();
return; }
}
bt_pic.setImageBitmap(photo);
}
public static void saveImage(Bitmap photo, String spath) {
try {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(spath, false));
photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (Exception e) {
e.printStackTrace();
return;
}
return ;
}
}