-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsell_new_item.java
231 lines (188 loc) · 8.04 KB
/
sell_new_item.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.example.asmid.pricetag;
import android.*;
import android.Manifest;
import android.content.Intent;
import android.content.Loader;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.android.internal.http.multipart.MultipartEntity;
import com.cloudinary.Cloudinary;
import com.cloudinary.utils.ObjectUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class sell_new_item extends AppCompatActivity {
private static final int CAMERA_REQUEST = 1888;
private static final int RESULT_LOAD_IMAGE = 1;
private static final int RESULT_UPLOADED = 2404;
private ImageView imageView;
private static String username;
private static File photoFile;
private static final int EXTERNAL_STORAGE = 0;
Cloudinary cloudinary;
Map result;
Bitmap photo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sell_new_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(Color.WHITE);
HashMap config = new HashMap();
config.put("cloud_name", "pricetag");
config.put("api_key", "679297912494948");
config.put("api_secret", "F04pIOgXGADgjIW7_FXQY-9yl6E");
cloudinary = new Cloudinary(config);
Intent thisIntent = getIntent();
username = thisIntent.getStringExtra("username");
this.imageView = (ImageView)this.findViewById(R.id.camera_imageView);
final Button photoButton = (Button) this.findViewById(R.id.button_camera);
photoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
Button galleryButton = (Button) this.findViewById(R.id.button_gallery);
galleryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMAGE);
}
});
FloatingActionButton uploadButton = (FloatingActionButton) findViewById(R.id.button_upload);
uploadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ImageView iv = (ImageView) findViewById(R.id.camera_imageView);
photo = ((BitmapDrawable)iv.getDrawable()).getBitmap();
if(photo==null){
Toast.makeText(getApplicationContext(),"No image selected!",Toast.LENGTH_SHORT).show();
}
else{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos);
uploadImagetask uploadTask = new uploadImagetask(cloudinary);
uploadTask.execute("");
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
if (requestCode == EXTERNAL_STORAGE) {
if (permissions.length == 1 &&
permissions[0] == Manifest.permission.WRITE_EXTERNAL_STORAGE &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
// Permission was denied. Display an error message.
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode,resultCode,data);
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
Bitmap scaledPhoto = Bitmap.createScaledBitmap(photo, 300,300,false);
imageView.setImageBitmap(scaledPhoto);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bitmap is required image which have to send in Bitmap form
String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(), photo, "title", null);
Uri uri = Uri.parse(path);
photoFile = new File(getRealPath(uri));
}
else if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
photoFile = new File(getRealPath(selectedImage));
ImageView imageView = (ImageView) findViewById(R.id.camera_imageView);
try {
Bitmap photo = MediaStore.Images.Media.getBitmap(getContentResolver(), selectedImage);
Bitmap scaledPhoto = Bitmap.createScaledBitmap(photo, 300,300,false);
imageView.setImageBitmap(scaledPhoto);
}
catch (Exception e){
e.printStackTrace();
}
}
else {
Log.d("success", data.getStringExtra("username"));
Intent returnIntent = new Intent(sell_new_item.this, OnSale.class);
returnIntent.putExtra("blah", "blah");
setResult(RESULT_OK, returnIntent);
finish();
}
}
public String getRealPath(Uri uri){
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
public void sendImageUrl(String url){
Intent intent = new Intent(sell_new_item.this, SellerItemD.class);
intent.putExtra("image", photo);
intent.putExtra("url", url);
intent.putExtra("username", username);
startActivityForResult(intent, RESULT_UPLOADED);
}
public class uploadImagetask extends AsyncTask<String, Void, String>{
Cloudinary mCloudinary;
public uploadImagetask(Cloudinary cloudinary){
super();
mCloudinary = cloudinary;
}
@Override
protected String doInBackground(String... params) {
String response = "";
try{
if(photoFile!=null){
result = mCloudinary.uploader().upload(photoFile, ObjectUtils.emptyMap());
response = (String)result.get("url");
}
}
catch(Exception e){
e.printStackTrace();
}
return response;
}
@Override
protected void onPostExecute(String result){
sendImageUrl(result);
Log.w("done", "done");
}
}
}