onActivityResult's intent.getPath() doesn't give me the correct filename











up vote
2
down vote

favorite
2












I am trying to fetch a file this way:



final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
String mimetypes = {"application/pdf"};
chooseFileIntent.setType("*/*");
chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
if (chooseFileIntent.resolveActivity(activity
.getApplicationContext().getPackageManager()) != null) {
chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
}


Then in onActivityResult :



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}


According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :




/document/acc=1;doc=28




So what to do? Thanks for your help.










share|improve this question


























    up vote
    2
    down vote

    favorite
    2












    I am trying to fetch a file this way:



    final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    String mimetypes = {"application/pdf"};
    chooseFileIntent.setType("*/*");
    chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
    if (chooseFileIntent.resolveActivity(activity
    .getApplicationContext().getPackageManager()) != null) {
    chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
    activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
    }


    Then in onActivityResult :



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    }


    According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :




    /document/acc=1;doc=28




    So what to do? Thanks for your help.










    share|improve this question
























      up vote
      2
      down vote

      favorite
      2









      up vote
      2
      down vote

      favorite
      2






      2





      I am trying to fetch a file this way:



      final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
      String mimetypes = {"application/pdf"};
      chooseFileIntent.setType("*/*");
      chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
      if (chooseFileIntent.resolveActivity(activity
      .getApplicationContext().getPackageManager()) != null) {
      chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
      activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
      }


      Then in onActivityResult :



      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      }


      According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :




      /document/acc=1;doc=28




      So what to do? Thanks for your help.










      share|improve this question













      I am trying to fetch a file this way:



      final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
      String mimetypes = {"application/pdf"};
      chooseFileIntent.setType("*/*");
      chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
      if (chooseFileIntent.resolveActivity(activity
      .getApplicationContext().getPackageManager()) != null) {
      chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
      activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
      }


      Then in onActivityResult :



      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      }


      According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :




      /document/acc=1;doc=28




      So what to do? Thanks for your help.







      android file






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 29 at 21:53









      Rob

      1,10221026




      1,10221026
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted











          I am trying to fetch a file




          Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.




          According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()




          That was never correct, though it tended to work on older versions of Android.




          So what to do?




          Well, that depends.



          If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT.



          If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:




          • If getScheme() of the Uri returns file, your original algorithm will work


          • If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user







          share|improve this answer





















          • Really great and complete answer! Thank you very much, DocumentFile.fromSingleUri() did the trick :)
            – Rob
            Jan 29 at 22:12










          • @CommonsWare, How to get the file path using DocumentFile ?
            – Amila Iddamalgoda
            Oct 7 at 12:37






          • 1




            @Amila You don't. You open an InputStream on the content identified by the Uri in the DocumentFile, and you consume the content that way.
            – CommonsWare
            Oct 7 at 12:47













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48510584%2fonactivityresults-intent-getpath-doesnt-give-me-the-correct-filename%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted











          I am trying to fetch a file




          Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.




          According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()




          That was never correct, though it tended to work on older versions of Android.




          So what to do?




          Well, that depends.



          If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT.



          If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:




          • If getScheme() of the Uri returns file, your original algorithm will work


          • If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user







          share|improve this answer





















          • Really great and complete answer! Thank you very much, DocumentFile.fromSingleUri() did the trick :)
            – Rob
            Jan 29 at 22:12










          • @CommonsWare, How to get the file path using DocumentFile ?
            – Amila Iddamalgoda
            Oct 7 at 12:37






          • 1




            @Amila You don't. You open an InputStream on the content identified by the Uri in the DocumentFile, and you consume the content that way.
            – CommonsWare
            Oct 7 at 12:47

















          up vote
          3
          down vote



          accepted











          I am trying to fetch a file




          Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.




          According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()




          That was never correct, though it tended to work on older versions of Android.




          So what to do?




          Well, that depends.



          If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT.



          If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:




          • If getScheme() of the Uri returns file, your original algorithm will work


          • If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user







          share|improve this answer





















          • Really great and complete answer! Thank you very much, DocumentFile.fromSingleUri() did the trick :)
            – Rob
            Jan 29 at 22:12










          • @CommonsWare, How to get the file path using DocumentFile ?
            – Amila Iddamalgoda
            Oct 7 at 12:37






          • 1




            @Amila You don't. You open an InputStream on the content identified by the Uri in the DocumentFile, and you consume the content that way.
            – CommonsWare
            Oct 7 at 12:47















          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted







          I am trying to fetch a file




          Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.




          According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()




          That was never correct, though it tended to work on older versions of Android.




          So what to do?




          Well, that depends.



          If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT.



          If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:




          • If getScheme() of the Uri returns file, your original algorithm will work


          • If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user







          share|improve this answer













          I am trying to fetch a file




          Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.




          According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()




          That was never correct, though it tended to work on older versions of Android.




          So what to do?




          Well, that depends.



          If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT.



          If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:




          • If getScheme() of the Uri returns file, your original algorithm will work


          • If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 29 at 22:05









          CommonsWare

          756k13618441897




          756k13618441897












          • Really great and complete answer! Thank you very much, DocumentFile.fromSingleUri() did the trick :)
            – Rob
            Jan 29 at 22:12










          • @CommonsWare, How to get the file path using DocumentFile ?
            – Amila Iddamalgoda
            Oct 7 at 12:37






          • 1




            @Amila You don't. You open an InputStream on the content identified by the Uri in the DocumentFile, and you consume the content that way.
            – CommonsWare
            Oct 7 at 12:47




















          • Really great and complete answer! Thank you very much, DocumentFile.fromSingleUri() did the trick :)
            – Rob
            Jan 29 at 22:12










          • @CommonsWare, How to get the file path using DocumentFile ?
            – Amila Iddamalgoda
            Oct 7 at 12:37






          • 1




            @Amila You don't. You open an InputStream on the content identified by the Uri in the DocumentFile, and you consume the content that way.
            – CommonsWare
            Oct 7 at 12:47


















          Really great and complete answer! Thank you very much, DocumentFile.fromSingleUri() did the trick :)
          – Rob
          Jan 29 at 22:12




          Really great and complete answer! Thank you very much, DocumentFile.fromSingleUri() did the trick :)
          – Rob
          Jan 29 at 22:12












          @CommonsWare, How to get the file path using DocumentFile ?
          – Amila Iddamalgoda
          Oct 7 at 12:37




          @CommonsWare, How to get the file path using DocumentFile ?
          – Amila Iddamalgoda
          Oct 7 at 12:37




          1




          1




          @Amila You don't. You open an InputStream on the content identified by the Uri in the DocumentFile, and you consume the content that way.
          – CommonsWare
          Oct 7 at 12:47






          @Amila You don't. You open an InputStream on the content identified by the Uri in the DocumentFile, and you consume the content that way.
          – CommonsWare
          Oct 7 at 12:47




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f48510584%2fonactivityresults-intent-getpath-doesnt-give-me-the-correct-filename%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          List item for chat from Array inside array React Native

          Thiostrepton

          Caerphilly