title: Upload pdf or txt in app android and convert to base64
body: <p>I'm working for an application in witch i need to upload a file ( image from camera or gallery, pdf or txt from documents), convert in base64 and add the String with the base64 in an array. I resolved for image both from camera and from gallery- Now i need to do the same with the documents. How ca I upload a document from my device to my app and convert it in base64? This is my code for images:</p>
  
  <pre><code>private void selectImage(){
  
      final CharSequence[] item={"Camera","Document","Cancel"};
  
      AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
      builder.setTitle("Carica");
      builder.setItems(item, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int i) {
              if(item[i].equals("Camera")){
                  Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                  startActivityForResult(intent,Request_camera);
              }
              else if(item[i].equals("Document")){
  
                  Intent intent3 = new Intent();
                  intent3.setType("*/*");
                  intent3.setAction(Intent.ACTION_GET_CONTENT);
                  startActivityForResult(Intent.createChooser(intent3,"Choose File to Upload.."),PICK_FILE_REQUEST);
                  Uri.parse(Environment.getExternalStorageDirectory().toString());
  
  
              }
  
              else if(item[i].equals("Cancel")){
                  dialog.dismiss();
              }
          }
      }).show();
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data){
      super.onActivityResult(requestCode,resultCode,data);
  
      if(resultCode== Activity.RESULT_OK){
          if(requestCode==Request_camera &amp;&amp; button1==1){
  
              Bundle bundle=data.getExtras();
              final Bitmap bpm= (Bitmap)bundle.get("data");
              uploadImage.setImageBitmap(bpm);
              bitmapToBase64(bpm);
  
              String mynewString=bitmapToBase64(bpm);
              mydocument.set(0,mynewString);
              decode64.setText(mynewString);
              getFileExt(uploadImage.toString());
  
          }
           else if(requestCode==Request_camera &amp;&amp; button1==2){
              Bundle bundle=data.getExtras();
              final Bitmap bpm= (Bitmap)bundle.get("data");
              imageView2.setImageBitmap(bpm);
              bitmapToBase64(bpm);
  
              String mynewString=bitmapToBase64(bpm);
              decodee2.setText(mynewString);
              decodee2.setTextColor(Color.GREEN);
          }
  
          else if(requestCode==Request_file){
              Uri selectImageUri=data.getData();
              uploadImage.setImageURI(selectImageUri);
              String newNome=getFileName(selectImageUri);
              decode64.setText(newNome);
  
              String realPath=selectImageUri.toString();
  
              decodee2.setText(realPath);
  
              File file=new File(newNome);
  
              getStringFile(file);
              String myDecode=getStringFile(file);
              mydocument.set(0,myDecode);
  
  
          }
      }
  
  }
  
  
  private String bitmapToBase64(Bitmap bitmap) {
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
      byte[] byteArray = byteArrayOutputStream .toByteArray();
      return Base64.encodeToString(byteArray, Base64.DEFAULT);
  }
  </code></pre>
  
  <p>Thanks!!</p>