{"id":448,"date":"2020-05-07T16:24:37","date_gmt":"2020-05-07T06:24:37","guid":{"rendered":"https:\/\/ghiassy.net\/?p=448"},"modified":"2020-05-07T16:26:43","modified_gmt":"2020-05-07T06:26:43","slug":"448","status":"publish","type":"post","link":"https:\/\/ghiassy.net\/?p=448","title":{"rendered":"Android File Picker with real File Path"},"content":{"rendered":"<p lang=\"en-US\"><span style=\"color: #00ff00;\">Hello,<\/span><\/p>\n<p lang=\"en-US\"><span style=\"color: #00ff00;\">Today I was looking for File Chooser for an android application I\u2019m working on. Unfortunately most of available methods does not return a correct file path to the file.<\/span><\/p>\n<p lang=\"en-US\"><!--more--><\/p>\n<pre class=\"lang:java decode:true \" title=\"MainActivity.java\"> public void btnClicked(View view)\r\n    {\r\n        Intent myFileIntent = new Intent(Intent.ACTION_GET_CONTENT);\r\n        myFileIntent.setType(\"*\/*\");\r\n        Intent.createChooser(myFileIntent, \"Choose a file\");\r\n        startActivityForResult(myFileIntent, 10);\r\n\r\n    }\r\n\r\n    @Override\r\n    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {\r\n        super.onActivityResult(requestCode, resultCode, data);\r\n\r\n\r\n        switch (requestCode){\r\n            case 10:\r\n                if(resultCode== RESULT_OK)\r\n                {\r\n                    Uri fileUri = data.getData();\r\n\r\n                    String path = FileUtils.getPath(this, fileUri);\r\n\r\n                    txtFilePath.setText(path);\r\n                    if(path == null || path.isEmpty())\r\n                        return;\r\n\r\n                    File file = new File(path);\r\n\r\n                    if(file.exists())\r\n                    {\r\n                        Toast.makeText(this, \"File Exist\", Toast.LENGTH_SHORT).show();\r\n\r\n                    }else{\r\n                        Toast.makeText(this, \"Cannot Access it!\", Toast.LENGTH_SHORT).show();\r\n\r\n                    }\r\n                }\r\n\r\n                break;\r\n        }\r\n    }<\/pre>\n<p>&nbsp;<\/p>\n<p lang=\"en-US\"><span style=\"color: #00ff00;\">I found this <strong><span style=\"font-size: 14pt;\"><a href=\"https:\/\/ghiassy.net\/wp-content\/uploads\/2020\/05\/FileUtils.java\">FileUtils.java<\/a>\u00a0<\/span><\/strong> file that really made it easy to access the the real file path of the file.<\/span><\/p>\n<p lang=\"en-US\"><span style=\"color: #00ff00;\">Hope this helps.<\/span><\/p>\n<p lang=\"en-US\"><span style=\"color: #00ff00;\">Best of luck<\/span><\/p>\n<p lang=\"en-US\"><span style=\"color: #00ff00;\">&#8211;Saeed \ud83d\ude42<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello, Today I was looking for File Chooser for an android application I\u2019m working on. Unfortunately most of available methods does not return a correct file path to the file.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,5,4],"tags":[],"class_list":["post-448","post","type-post","status-publish","format-standard","hentry","category-android","category-java","category-programming"],"_links":{"self":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts\/448","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=448"}],"version-history":[{"count":4,"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts\/448\/revisions"}],"predecessor-version":[{"id":453,"href":"https:\/\/ghiassy.net\/index.php?rest_route=\/wp\/v2\/posts\/448\/revisions\/453"}],"wp:attachment":[{"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=448"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=448"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ghiassy.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=448"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}