{"id":62,"date":"2016-04-01T11:36:16","date_gmt":"2016-04-01T09:36:16","guid":{"rendered":"http:\/\/antek.atthost24.pl\/?p=62"},"modified":"2016-03-30T22:22:49","modified_gmt":"2016-03-30T20:22:49","slug":"62","status":"publish","type":"post","link":"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/","title":{"rendered":"Android file chooser dialog"},"content":{"rendered":"<p>API Androida niestety nie dostarcza nam w standardzie \u017cadnego mechanizmu umo\u017cliwiaj\u0105cego wyb\u00f3r pliku z systemu plik\u00f3w. W takim przypadku nie pozostaje nam nic innego jak zaimplementowa\u0107 to samemu lub skorzysta\u0107 z gotowej biblioteki.<\/p>\n<p>Postanowi\u0142em bazowa\u0107 na file chooser&#8217;ze zaimplementowanym przez Rogera Keaysa. Jest on dost\u0119pny pod poni\u017cszym adresem:\u00a0<a href=\"https:\/\/rogerkeays.com\/simple-android-file-chooser\">https:\/\/rogerkeays.com\/simple-android-file-chooser<\/a>.<\/p>\n<p>Moja implementacja wygl\u0105da w nast\u0119puj\u0105cy spos\u00f3b:<\/p>\n<pre>class FileChooser(val activity: Activity, val fileListener: FileSelectedListener, val maxFileSize: Double) {\r\n  private lazy val messageMaker = wire(classOf[MessageMaker])\r\n  private val parentDir = \"..\"\r\n  private val mb = 1024.0 * 1024.0\r\n  private val dialog = new Dialog(activity)\r\n  private val list = new ListView(activity)\r\n  private var currentPath: File = _\r\n\r\n  list.setOnItemClickListener(new AdapterView.OnItemClickListener() {\r\n    override def onItemClick(parent: AdapterView[_], view: View, position: Int, id: Long): Unit = {\r\n      val fileChosen: String = list.getItemAtPosition(position).asInstanceOf[String]\r\n      val chosenFile: File = getChosenFile(fileChosen)\r\n      if (chosenFile.isDirectory) {\r\n        refresh(chosenFile)\r\n      }\r\n      else {\r\n        val sizeInMB: Double = chosenFile.length \/ mb\r\n        if (sizeInMB &gt; maxFileSize) {\r\n          messageMaker.info(\"Selected file is too large\")\r\n        }\r\n        else {\r\n          if (fileListener != null) {\r\n            fileListener.fileSelected(chosenFile)\r\n          }\r\n          dialog.dismiss()\r\n        }\r\n      }\r\n    }\r\n  })\r\n  dialog.setContentView(list)\r\n  dialog.getWindow.setLayout(MATCH_PARENT, MATCH_PARENT)\r\n  refresh(Environment.getExternalStorageDirectory)\r\n\r\n  def showDialog() {\r\n    dialog.show()\r\n  }\r\n\r\n  \/**\r\n   * Sort, filter and display the files for the given path.\r\n   *\/\r\n  private def refresh(path: File) {\r\n    this.currentPath = path\r\n    if (path.exists) {\r\n      val dirs: Array[File] = path.listFiles(new FileFilter() {\r\n        def accept(file: File): Boolean = {\r\n          file.isDirectory &amp;&amp; file.canRead\r\n        }\r\n      })\r\n      val files: Array[File] = path.listFiles(new FileFilter() {\r\n        def accept(file: File): Boolean = {\r\n          !file.isDirectory &amp;&amp; file.canRead\r\n        }\r\n      })\r\n      dirs.sortWith(_.getName &gt; _.getName)\r\n      files.sortWith(_.getName &gt; _.getName)\r\n\r\n      \/\/ convert to an array\r\n      val i: Int = 0\r\n      var fileList = ArrayBuffer[String]()\r\n\r\n      if (path.getParentFile != null) {\r\n        fileList += parentDir\r\n      }\r\n      dirs.foreach(d =&gt; fileList += d.getName)\r\n      files.foreach(f =&gt; fileList += f.getName)\r\n\r\n      \/\/ refresh the user interface\r\n      dialog.setTitle(currentPath.getPath)\r\n      list.setAdapter(new ArrayAdapter(activity, android.R.layout.simple_list_item_1, fileList.toArray) {\r\n        override def getView(pos: Int, view: View, parent: ViewGroup): View = {\r\n          val superView = super.getView(pos, view, parent)\r\n          superView.asInstanceOf[TextView].setSingleLine(true)\r\n          superView\r\n        }\r\n      })\r\n    }\r\n  }\r\n\r\n  private def getChosenFile(fileChosen: String): File = {\r\n    if (fileChosen == parentDir) {\r\n      currentPath.getParentFile\r\n    }\r\n    else {\r\n      new File(currentPath, fileChosen)\r\n    }\r\n  }\r\n}<\/pre>\n<pre>trait FileSelectedListener {\r\n  def fileSelected(file: File)\r\n}<\/pre>\n<p>G\u0142\u00f3wn\u0105 zmian\u0105 (oczywi\u015bcie poza napisaniem tego w j\u0119zyku Scala) jest dodanie ograniczenia na maksymaln\u0105 wielko\u015b\u0107 pliku. Po wyborze pliku, je\u017celi jego rozmiar przekracza ustawiony maksymalny rozmiar to zostaje wy\u015bwietlony komunkat z informacj\u0105, a plik nie zostaje dodany.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>API Androida niestety nie dostarcza nam w standardzie \u017cadnego mechanizmu umo\u017cliwiaj\u0105cego wyb\u00f3r pliku z systemu plik\u00f3w. W takim przypadku nie pozostaje nam nic innego jak zaimplementowa\u0107 to samemu&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,6],"tags":[5,3,4],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-daj-sie-poznac","category-simple-document-manager","tag-android","tag-dajsiepoznac","tag-sdm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android file chooser dialog - Lukasz Antkowiak&#039;s blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/\" \/>\n<meta property=\"og:locale\" content=\"pl_PL\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android file chooser dialog - Lukasz Antkowiak&#039;s blog\" \/>\n<meta property=\"og:description\" content=\"API Androida niestety nie dostarcza nam w standardzie \u017cadnego mechanizmu umo\u017cliwiaj\u0105cego wyb\u00f3r pliku z systemu plik\u00f3w. W takim przypadku nie pozostaje nam nic innego jak zaimplementowa\u0107 to samemu...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/\" \/>\n<meta property=\"og:site_name\" content=\"Lukasz Antkowiak&#039;s blog\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-01T09:36:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-03-30T20:22:49+00:00\" \/>\n<meta name=\"author\" content=\"\u0141ukasz Antkowiak\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Napisane przez\" \/>\n\t<meta name=\"twitter:data1\" content=\"\u0141ukasz Antkowiak\" \/>\n\t<meta name=\"twitter:label2\" content=\"Szacowany czas czytania\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minuty\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/\",\"url\":\"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/\",\"name\":\"Android file chooser dialog - Lukasz Antkowiak&#039;s blog\",\"isPartOf\":{\"@id\":\"https:\/\/blog.lantkowiak.pl\/#website\"},\"datePublished\":\"2016-04-01T09:36:16+00:00\",\"dateModified\":\"2016-03-30T20:22:49+00:00\",\"author\":{\"@id\":\"https:\/\/blog.lantkowiak.pl\/#\/schema\/person\/009bfc4db220c225786c59b1748ba1e6\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/#breadcrumb\"},\"inLanguage\":\"pl-PL\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Strona g\u0142\u00f3wna\",\"item\":\"https:\/\/blog.lantkowiak.pl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android file chooser dialog\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.lantkowiak.pl\/#website\",\"url\":\"https:\/\/blog.lantkowiak.pl\/\",\"name\":\"Lukasz Antkowiak&#039;s blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.lantkowiak.pl\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"pl-PL\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.lantkowiak.pl\/#\/schema\/person\/009bfc4db220c225786c59b1748ba1e6\",\"name\":\"\u0141ukasz Antkowiak\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pl-PL\",\"@id\":\"https:\/\/blog.lantkowiak.pl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e18e2bfe319da9b5757cf7f7ae1bfecf?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e18e2bfe319da9b5757cf7f7ae1bfecf?s=96&d=monsterid&r=g\",\"caption\":\"\u0141ukasz Antkowiak\"},\"description\":\"dd\",\"url\":\"https:\/\/blog.lantkowiak.pl\/index.php\/author\/antek\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android file chooser dialog - Lukasz Antkowiak&#039;s blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/","og_locale":"pl_PL","og_type":"article","og_title":"Android file chooser dialog - Lukasz Antkowiak&#039;s blog","og_description":"API Androida niestety nie dostarcza nam w standardzie \u017cadnego mechanizmu umo\u017cliwiaj\u0105cego wyb\u00f3r pliku z systemu plik\u00f3w. W takim przypadku nie pozostaje nam nic innego jak zaimplementowa\u0107 to samemu...","og_url":"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/","og_site_name":"Lukasz Antkowiak&#039;s blog","article_published_time":"2016-04-01T09:36:16+00:00","article_modified_time":"2016-03-30T20:22:49+00:00","author":"\u0141ukasz Antkowiak","twitter_card":"summary_large_image","twitter_misc":{"Napisane przez":"\u0141ukasz Antkowiak","Szacowany czas czytania":"2 minuty"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/","url":"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/","name":"Android file chooser dialog - Lukasz Antkowiak&#039;s blog","isPartOf":{"@id":"https:\/\/blog.lantkowiak.pl\/#website"},"datePublished":"2016-04-01T09:36:16+00:00","dateModified":"2016-03-30T20:22:49+00:00","author":{"@id":"https:\/\/blog.lantkowiak.pl\/#\/schema\/person\/009bfc4db220c225786c59b1748ba1e6"},"breadcrumb":{"@id":"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/#breadcrumb"},"inLanguage":"pl-PL","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blog.lantkowiak.pl\/index.php\/2016\/04\/01\/62\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Strona g\u0142\u00f3wna","item":"https:\/\/blog.lantkowiak.pl\/"},{"@type":"ListItem","position":2,"name":"Android file chooser dialog"}]},{"@type":"WebSite","@id":"https:\/\/blog.lantkowiak.pl\/#website","url":"https:\/\/blog.lantkowiak.pl\/","name":"Lukasz Antkowiak&#039;s blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.lantkowiak.pl\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"pl-PL"},{"@type":"Person","@id":"https:\/\/blog.lantkowiak.pl\/#\/schema\/person\/009bfc4db220c225786c59b1748ba1e6","name":"\u0141ukasz Antkowiak","image":{"@type":"ImageObject","inLanguage":"pl-PL","@id":"https:\/\/blog.lantkowiak.pl\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e18e2bfe319da9b5757cf7f7ae1bfecf?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e18e2bfe319da9b5757cf7f7ae1bfecf?s=96&d=monsterid&r=g","caption":"\u0141ukasz Antkowiak"},"description":"dd","url":"https:\/\/blog.lantkowiak.pl\/index.php\/author\/antek\/"}]}},"_links":{"self":[{"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":3,"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":65,"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions\/65"}],"wp:attachment":[{"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lantkowiak.pl\/index.php\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}