Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // open folder method
- public void openFolder() {
- Intent intent = new Intent();
- intent.setType("*/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_CHOOSE_DOCUMENTS);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- switch (requestCode) {
- case REQUEST_CHOOSE_DOCUMENTS:
- if (resultCode == RESULT_OK) {
- isAttachApply = true;
- uri = data.getData();
- try {
- InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
- if (inputStream != null) {
- String name = fileName.substring(0, fileName.length() - 4);
- String suffix = fileName.substring(fileName.length() - 4);
- attachFile = new File(getContext().getCacheDir(), name + suffix);
- attachFile = copyInputStreamToFile(inputStream, attachFile);
- if (attachFile.length() == 0) {
- setWarningMessage(getString(R.string.apply_warning_attach_file_null_size));
- } else if (attachFile.length() >= MAX_FILE_SIZE) {
- setWarningMessage(getString(R.string.apply_warning_attach_file_big_size));
- } else {
- if (adapter.getItemCount() > 0) {
- adapter.setItemsNotActive(true);
- adapter.notifyDataSetChanged();
- }
- attachFileTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.blue));
- warningAttachMessage.setVisibility(View.GONE);
- attachFileContainer.setBackgroundResource(R.drawable.blue_border_chosen_bg);
- isHasAttachWarning = false;
- }
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- }
- }
- // create file inputstream
- private File copyInputStreamToFile(InputStream in, File file) {
- OutputStream out = null;
- try {
- out = new FileOutputStream(file);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) > 0) {
- out.write(buf, 0, len);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return file;
- }
- String mimeType = getContext().getContentResolver().getType(uri);
- MediaType type = MediaType.parse(mimeType);
- MultipartBody.Part body = MultipartBody.Part.createFormData("file", attachFile.getName(), RequestBody.create(type, attachFile));
- Observable<Response<JsonObject>> observable = Api.get(getContext()).applyAttachRx(applyModel, body);
- // interface
- @Multipart
- @POST("vacancy/applyattach")
- Observable<Response<JsonObject>> applyAttachRx(@Part("model") ApplyModel applyModel, @Part() MultipartBody.Part file);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement