onActivityResult's intent.getPath() doesn't give me the correct filename
up vote
2
down vote
favorite
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.
add a comment |
up vote
2
down vote
favorite
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.
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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.
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.
asked Jan 29 at 21:53
Rob
1,10221026
1,10221026
add a comment |
add a comment |
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 theUrireturnsfile, your original algorithm will workIf
getScheme()of theUrireturnscontent, useDocumentFile.fromSingleUri()to create aDocumentFile, then callgetName()on thatDocumentFile— this should return a "display name" which should be recognizable to the user
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 anInputStreamon the content identified by theUriin theDocumentFile, and you consume the content that way.
– CommonsWare
Oct 7 at 12:47
add a comment |
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 theUrireturnsfile, your original algorithm will workIf
getScheme()of theUrireturnscontent, useDocumentFile.fromSingleUri()to create aDocumentFile, then callgetName()on thatDocumentFile— this should return a "display name" which should be recognizable to the user
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 anInputStreamon the content identified by theUriin theDocumentFile, and you consume the content that way.
– CommonsWare
Oct 7 at 12:47
add a comment |
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 theUrireturnsfile, your original algorithm will workIf
getScheme()of theUrireturnscontent, useDocumentFile.fromSingleUri()to create aDocumentFile, then callgetName()on thatDocumentFile— this should return a "display name" which should be recognizable to the user
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 anInputStreamon the content identified by theUriin theDocumentFile, and you consume the content that way.
– CommonsWare
Oct 7 at 12:47
add a comment |
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 theUrireturnsfile, your original algorithm will workIf
getScheme()of theUrireturnscontent, useDocumentFile.fromSingleUri()to create aDocumentFile, then callgetName()on thatDocumentFile— this should return a "display name" which should be recognizable to the user
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 theUrireturnsfile, your original algorithm will workIf
getScheme()of theUrireturnscontent, useDocumentFile.fromSingleUri()to create aDocumentFile, then callgetName()on thatDocumentFile— this should return a "display name" which should be recognizable to the user
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 anInputStreamon the content identified by theUriin theDocumentFile, and you consume the content that way.
– CommonsWare
Oct 7 at 12:47
add a comment |
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 anInputStreamon the content identified by theUriin theDocumentFile, 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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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