I've added a button to my application which is supposed to open the download folder of the phone, and from there you should be able to click on files that were stored there, from the same app. Right now im saving some data there.
Problem is; I cant open the saved files in the folder. I can see the files stored right there, but when I press one of them you immediatley go back to the app and not the file that you pressed.
Is there something I'm missing? Are you not supposed to open files stored in external storage from another app?
I've tried adding permissions in manifest and checkSelfpermission for checks in runtime, but with no success.
Here's the button for opening download folder:
private void openSavedLocation(){
if (ContextCompat.checkSelfPermission(ExportAndImport.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(ExportAndImport.this, new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
intent.setDataAndType(uri, "text/xml");
startActivity(Intent.createChooser(intent, "Open Folder"));}
I can open the file perfectly when Im opening it outside the app, not via this "createChooser". What could i be missing? Any help is appreciated.
but when I press one of them you immediatley go back to the app and not the file that you pressed
That is what your code does. ACTION_GET_CONTENT
says "let the user pick a piece of content". It does not say "open that piece of content in some other app". There is no single Intent
action for saying "let the user pick a piece of content, then open that piece of content in some other app".
Is there something I'm missing?
If you want to try to open the XML in some other app:
startActivityForResult()
, not startActivity()
, for your ACTION_GET_CONTENT
request (and get rid of the createChooser()
bit) onActivityResult()
to get the result of the user's choice RESULT_OK
in onActivityResult()
), create an ACTION_VIEW
Intent
wrapped around the Uri
that you get from the Intent
passed into onActivityResult()
, and call startActivity()
on the ACTION_VIEW
Intent
If, instead, your objective is to open this XML in your app, you would:
startActivityForResult()
, not startActivity()
, for your ACTION_GET_CONTENT
request (and get rid of the createChooser()
bit) onActivityResult()
to get the result of the user's choice RESULT_OK
in onActivityResult()
), get the Uri
of the content from the Intent
passed into onActivityResult()
, then use ContentResolver
to do something useful with that Uri
(eg, openInputStream()
to read in the content) Here's the button for opening download folder
ACTION_GET_CONTENT
uses the MIME type. It will not necessarily honor your supplied starting Uri
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.