title: Browse pdf from phone in Android Studio
body: <p>I need to create an app that involves browsing pdf from phone and then reading it (which will be executed later, once the browsing part is done) and I'm having trouble with it's code. I have gone through the following link I found on stackoverflow, but unfortunately it is not working,
  <a href="https://stackoverflow.com/questions/37302143/browse-and-upload-pdf-or-word-file-in-android/37309153#37309153?newreg=96d2efbe54044a1b840785c91f4df238">Browse and upload pdf or word file in Android</a>
  Here is my MainActivity.java class</p>
  
  <pre><code>import java.io.File;
  import android.net.Uri;
  import android.os.Bundle;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Toast;
  import android.app.Activity;
  import android.content.ActivityNotFoundException;
  import android.content.Intent;
  import android.content.Context;
  import android.content.Intent;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.Bundle;
  import android.provider.MediaStore;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.widget.Button;
  import java.io.File;
  import java.net.URI;
  import java.net.URISyntaxException;
  
  public class MainActivity extends AppCompatActivity {
  
  Button btnAttach;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      btnAttach = (Button) findViewById(R.id.button1);    
      //button1 is the id of the only button I have in my activity_main.xml
  
      // view products click event
      btnAttach.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View view) {
              button1_OnClick(view);
              // Launching All products Activity
  
          }
      });
  }
  
  
  public void button1_OnClick(View view) {
      getDocument();
  }
  
  
  private void getDocument() {
      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
      intent.setType("application/msword,application/pdf");
      intent.addCategory(Intent.CATEGORY_OPENABLE);
      }
  
  }
  protected void onActivityResult ( int req, int result, Intent data)
  {
      // TODO Auto-generated method stub
      super.onActivityResult(req, result, data);
      if (result == RESULT_OK) {
          Uri fileuri = data.getData();
       String   docFilePath = getFileNameByUri(this, fileuri);
      }
  }
  
  // get file path
  
  private String getFileNameByUri(Context context, Uri uri) {
      String filepath = "";//default fileName
      File file;
      if (uri.getScheme().toString().compareTo("content") == 0) {
          Cursor cursor = context.getContentResolver().query(uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA, MediaStore.Images.Media.ORIENTATION}, null, null, null);
          int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  
          cursor.moveToFirst();
  
          String mImagePath = cursor.getString(column_index);
          cursor.close();
          filepath = mImagePath;
  
      } else if (uri.getScheme().compareTo("file") == 0) {
          try {
              file = new File(new URI(uri.toString()));
              if (file.exists())
                  filepath = file.getAbsolutePath();
  
          } catch (URISyntaxException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      } else {
          filepath = uri.getPath();
      }
      return filepath;
  }
  }
  </code></pre>