-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFragment
More file actions
248 lines (183 loc) · 8.69 KB
/
Fragment
File metadata and controls
248 lines (183 loc) · 8.69 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//Fragment for upload image
@SuppressLint("ValidFragment")
public class ImageFragment extends Fragment {
private int STORAGE_PERMISSION_CODE = 23;
private static final int REQUEST_GALLERY_CODE_FRONT = 200, REQUEST_GALLERY_CODE_BACK = 201;
private static final int REQUEST_GALLERY_CODE_FRONT_C = 300, REQUEST_GALLERY_CODE_BACK_C = 301;
String filePath_1, filePath_2;
ApiService apiService;
Button btn_upload_image,selectOneImage,SelectSecondImage;
public ImageFragment() {
apiService = ApiUtilities.getApiservice();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.imagefragment, container, false);
btn_upload_image=view.findViewById(R.id.btn_upload_image);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
selectOneImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isReadStorageAllowed()) {
selectImage(REQUEST_GALLERY_CODE_FRONT, REQUEST_GALLERY_CODE_FRONT_C);
} else {
requestStoragePermission();
}
}
});
SelectSecondImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isReadStorageAllowed()) {
selectImage(REQUEST_GALLERY_CODE_BACK, REQUEST_GALLERY_CODE_BACK_C);
} else {
requestStoragePermission();
}
}
});
btn_upload_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (filePath_1 == null) {
Toast.makeText(getActivity(), "Select fist image", Toast.LENGTH_SHORT).show();
} else if (filePath_2 == null) {
Toast.makeText(getActivity(), "Select second image", Toast.LENGTH_SHORT).show();
} else {
upload();
}
}
});
}
private void selectImage(final int REQUEST_CODE, final int CAMERA_CODE) {
// SyncStateContract.Constants.iscamera = true;
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
TextView title = new TextView(getActivity());
title.setText("SELECT ONE");
title.setBackgroundColor(getActivity().getResources().getColor(R.color.blue));
title.setPadding(10, 15, 15, 15);
title.setGravity(Gravity.CENTER);
title.setTextColor(Color.WHITE);
title.setTextSize(22);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCustomTitle(title);
// builder.setTitle("Add Photo!");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
openBackCamera(CAMERA_CODE);
} else if (items[item].equals("Choose from Library")) {
Intent openGalleryIntent = new Intent(Intent.ACTION_PICK);
openGalleryIntent.setType("image/*");
startActivityForResult(openGalleryIntent, REQUEST_CODE);
} else if (items[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
void upload() {
ArrayList<String> filePaths = new ArrayList<>();
filePaths.add(filePath_frant);
filePaths.add(filePath_back);
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);
for (int i = 0; i < filePaths.size(); i++) {
File file = new File(filePaths.get(i));
builder.addFormDataPart("file[]", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
}
final MultipartBody requestBody = builder.build();
Toast.makeText(getActivity(), "" + requestBody, Toast.LENGTH_SHORT).show();
Call<ResponseBody> call = apiService.uploadImage(requestBody);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
Toast.makeText(getActivity(), "Uploaded", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.d("", "Error " + t.getMessage());
}
});
}
private void openBackCamera(int CAMERA_REQUEST) {
if (CAMERA_REQUEST == 300) {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
filePath_frant = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(filePath_frant);
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
filePath_back = storageDir.getAbsolutePath() + "/" + imageFileName;
File file = new File(filePath_back);
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
/*Permission*/
private void requestStoragePermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)) {
}
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
Toast.makeText(getActivity(), "Permission denied ", Toast.LENGTH_LONG).show();
}
}
}
private boolean isReadStorageAllowed() {
//Getting the permission status
int result = ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
//If permission is granted returning true
if (result == PackageManager.PERMISSION_GRANTED)
return true;
//If permission is not granted returning false
return false;
}
@SuppressLint("NewApi")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_GALLERY_CODE_FRONT && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
filePath_frant = getRealPathFromURIPath(uri, getActivity());
} else if (requestCode == REQUEST_GALLERY_CODE_BACK && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
filePath_back = getRealPathFromURIPath(uri, getActivity());
}
}
private String getRealPathFromURIPath(Uri contentURI, Activity activity) {
Cursor cursor = activity.getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) {
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
}