Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. // open folder method
  2.     public void openFolder() {
  3.         Intent intent = new Intent();
  4.         intent.setType("*/*");
  5.         intent.setAction(Intent.ACTION_GET_CONTENT);
  6.         startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_CHOOSE_DOCUMENTS);
  7.     }
  8.  
  9.  @Override
  10.  public void onActivityResult(int requestCode, int resultCode, Intent data) {
  11.  switch (requestCode) {
  12.  case REQUEST_CHOOSE_DOCUMENTS:
  13.                 if (resultCode == RESULT_OK) {
  14.                     isAttachApply = true;
  15.  
  16.                     uri = data.getData();
  17. try {
  18.                                 InputStream inputStream = getContext().getContentResolver().openInputStream(uri);
  19.  
  20.                                 if (inputStream != null) {
  21.                                     String name = fileName.substring(0, fileName.length() - 4);
  22.                                     String suffix = fileName.substring(fileName.length() - 4);
  23.  
  24.                                     attachFile = new File(getContext().getCacheDir(), name + suffix);
  25.                                     attachFile = copyInputStreamToFile(inputStream, attachFile);
  26.  
  27.                                     if (attachFile.length() == 0) {
  28.                                         setWarningMessage(getString(R.string.apply_warning_attach_file_null_size));
  29.                                     } else if (attachFile.length() >= MAX_FILE_SIZE) {
  30.                                         setWarningMessage(getString(R.string.apply_warning_attach_file_big_size));
  31.                                     } else {
  32.                                         if (adapter.getItemCount() > 0) {
  33.                                             adapter.setItemsNotActive(true);
  34.                                             adapter.notifyDataSetChanged();
  35.                                         }
  36.                                         attachFileTitle.setTextColor(ContextCompat.getColor(getContext(), R.color.blue));
  37.                                         warningAttachMessage.setVisibility(View.GONE);
  38.                                         attachFileContainer.setBackgroundResource(R.drawable.blue_border_chosen_bg);
  39.                                         isHasAttachWarning = false;
  40.                                     }
  41.                                 }
  42.                             } catch (FileNotFoundException e) {
  43.                                 e.printStackTrace();
  44.                             }
  45.                         }
  46.                     }
  47.                 }
  48.  
  49. // create file inputstream
  50.     private File copyInputStreamToFile(InputStream in, File file) {
  51.         OutputStream out = null;
  52.  
  53.         try {
  54.             out = new FileOutputStream(file);
  55.             byte[] buf = new byte[1024];
  56.             int len;
  57.  
  58.             while ((len = in.read(buf)) > 0) {
  59.                 out.write(buf, 0, len);
  60.             }
  61.  
  62.         } catch (Exception e) {
  63.             e.printStackTrace();
  64.         } finally {
  65.             try {
  66.                 if (out != null) {
  67.                     out.close();
  68.                 }
  69.                 in.close();
  70.             } catch (IOException e) {
  71.                 e.printStackTrace();
  72.             }
  73.         }
  74.         return file;
  75.     }
  76. String mimeType = getContext().getContentResolver().getType(uri);
  77. MediaType type = MediaType.parse(mimeType);
  78. MultipartBody.Part body = MultipartBody.Part.createFormData("file", attachFile.getName(), RequestBody.create(type, attachFile));
  79. Observable<Response<JsonObject>> observable = Api.get(getContext()).applyAttachRx(applyModel, body);
  80.  
  81. // interface
  82.   @Multipart
  83.   @POST("vacancy/applyattach")
  84.   Observable<Response<JsonObject>> applyAttachRx(@Part("model") ApplyModel applyModel, @Part() MultipartBody.Part file);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement