Site icon C1CTech

Android read a file from assets in java/Kotlin

<p>This article is about how to read a file from the assets folder in android in both languages Java&sol;Kotlin&period;<&sol;p>&NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"nJzWkv9CgqQ" title&equals;"Android read a file from assets in java&sol;Kotlin"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;nJzWkv9CgqQ"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;nJzWkv9CgqQ&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android read a file from assets in java&sol;Kotlin"><&sol;a><&sol;amp-youtube><&sol;p>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Creating Assets folder and a file<&sol;span><&sol;strong><&sol;h4>&NewLine;<p class&equals;"p2">1&period; To create an <span style&equals;"color&colon; &num;008000&semi;"><b>asset<&sol;b><&sol;span> folder in Android studio open your project in <strong><span style&equals;"color&colon; &num;008000&semi;">Android<&sol;span><&sol;strong> mode&period; Go to the <span style&equals;"color&colon; &num;008000&semi;"><b>app &gt&semi; right-click &gt&semi; New &gt&semi; Folder &gt&semi; Assets Folder<&sol;b><&sol;span>&period;<&sol;p>&NewLine;<p><img class&equals;"wp-image-2748 aligncenter" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screen-Shot-2021-07-22-at-3&period;21&period;44-PM-1024x608&period;png" alt&equals;"" width&equals;"637" height&equals;"378" &sol;><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<p class&equals;"p1">2&period; Android Studio will open a dialog box&period; Under the<span style&equals;"color&colon; &num;008000&semi;"><strong> Target Source<&sol;strong> <strong>Set<&sol;strong><&sol;span>&comma; option <span style&equals;"color&colon; &num;0000ff&semi;"><b>main<&sol;b><&sol;span> should be selected&comma; and click on the <span style&equals;"color&colon; &num;0000ff&semi;"><b>finish <&sol;b><&sol;span>button&period;<&sol;p>&NewLine;<p class&equals;"p1">3&period; Right-click on the <strong><span style&equals;"color&colon; &num;008000&semi;">assets<&sol;span><&sol;strong> folder&comma; select <span style&equals;"color&colon; &num;008000&semi;"><strong>New &gt&semi;&gt&semi; file &lpar;myText&period;txt&rpar;&comma;<&sol;strong><&sol;span> and add your text&period;<&sol;p>&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>myText&period;txt<&sol;strong><&sol;span><&sol;p>&NewLine;<pre>"All our dreams can come true if we have the courage to pursue them"<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Creating the XML file<&sol;span><&sol;strong><&sol;h4>&NewLine;<p>4 &period; The below layout file consists of a Button and a TextView&period;<&sol;p>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;reading&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"30dp"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;padding&equals;"15dp"&NewLine; android&colon;text&equals;"Reading text from Assets"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine; &lt&semi;TextView&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;text"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginTop&equals;"20dp"&NewLine; android&colon;gravity&equals;"center"&NewLine; android&colon;padding&equals;"15dp"&NewLine; android&colon;textColor&equals;"&commat;color&sol;colorPrimaryDark"&NewLine; android&colon;textSize&equals;"25sp"&NewLine; android&colon;textStyle&equals;"bold"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;reading&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;reading&lowbar;text"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;reading&lowbar;text" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Implementing code in Activity<&sol;strong><&sol;span><&sol;h4>&NewLine;<p>5 &period; In MainActivity&comma; on-click of the button&comma; we will read the text file &lpar;myText&period;txt&rpar; from the assets folder and convert it into a String&comma; and then finally set it in a textview&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Java code&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;c1ctech&period;readassetsfileexp&semi;&NewLine;&NewLine;import android&period;os&period;Bundle&semi;&NewLine;import android&period;view&period;View&semi;&NewLine;import android&period;widget&period;Button&semi;&NewLine;import android&period;widget&period;TextView&semi;&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;import java&period;io&period;IOException&semi;&NewLine;import java&period;io&period;InputStream&semi;&NewLine;&NewLine;public class MainActivity extends AppCompatActivity &lbrace;&NewLine;&NewLine; Button btnReadingText&semi;&NewLine; TextView tvText&semi;&NewLine;&NewLine; &commat;Override&NewLine; protected void onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main2&rpar;&semi;&NewLine;&NewLine; tvText &equals; findViewById&lpar;R&period;id&period;tv&lowbar;text&rpar;&semi;&NewLine;&NewLine; btnReadingText &equals; findViewById&lpar;R&period;id&period;btn&lowbar;reading&lowbar;text&rpar;&semi;&NewLine; btnReadingText&period;setOnClickListener&lpar;new View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; public void onClick&lpar;View view&rpar; &lbrace;&NewLine;&NewLine; readFromAsset&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;read file myText&period;txt from assets folder<&sol;span><&sol;strong>&NewLine; private void readFromAsset&lpar;&rpar; &lbrace;&NewLine; String string &equals; ""&semi;&NewLine;&NewLine; try &lbrace;&NewLine; InputStream inputStream &equals; getAssets&lpar;&rpar;&period;open&lpar;"myText&period;txt"&rpar;&semi;&NewLine; int size &equals; inputStream&period;available&lpar;&rpar;&semi;&NewLine; byte&lbrack;&rsqb; buffer &equals; new byte&lbrack;size&rsqb;&semi;&NewLine; inputStream&period;read&lpar;buffer&rpar;&semi;&NewLine; string &equals; new String&lpar;buffer&rpar;&semi;&NewLine; &rcub; catch &lpar;IOException e&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine; tvText&period;setText&lpar;string&rpar;&semi;&NewLine; &rcub;&NewLine;&NewLine;&rcub;<&sol;pre>&NewLine;<h4><strong><span style&equals;"color&colon; &num;000080&semi;">Kotlin code&colon;<&sol;span><&sol;strong><&sol;h4>&NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;kt<&sol;span><&sol;strong><&sol;p>&NewLine;<pre>package com&period;c1ctech&period;readassetsfileexp&NewLine;&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&NewLine;import android&period;os&period;Bundle&NewLine;import kotlinx&period;android&period;synthetic&period;main&period;activity&lowbar;main&period;&ast;&NewLine;import java&period;io&period;IOException&NewLine;&NewLine;class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;&NewLine;&NewLine; override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&NewLine;&NewLine; btn&lowbar;reading&lowbar;text&period;setOnClickListener &lbrace;&NewLine; readFromAsset&lpar;&rpar;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;read file myText&period;txt from assets folder<&sol;span><&sol;strong>&NewLine; private fun readFromAsset&lpar;&rpar; &lbrace;&NewLine; var string &equals; ""&NewLine; try &lbrace;&NewLine; val inputStream &equals; assets&period;open&lpar;"myText&period;txt"&rpar;&NewLine; val size&colon; Int &equals; inputStream&period;available&lpar;&rpar;&NewLine; val buffer &equals; ByteArray&lpar;size&rpar;&NewLine; inputStream&period;read&lpar;buffer&rpar;&period;toString&lpar;&rpar;&NewLine;&NewLine; string &equals; String&lpar;buffer&rpar;&NewLine;&NewLine; &rcub; catch &lpar;e&colon; IOException&rpar; &lbrace;&NewLine; e&period;printStackTrace&lpar;&rpar;&NewLine; &rcub;&NewLine; tv&lowbar;text&period;text &equals; string&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;<p>Run the app and click on the button&period; You can see the file text displayed in the textview&period;<&sol;p>&NewLine;<p><img class&equals;"alignnone wp-image-2749" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1626949009-576x1024&period;png" alt&equals;"" width&equals;"327" height&equals;"581" &sol;>      <img class&equals;"alignnone wp-image-2750" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;07&sol;Screenshot&lowbar;1626948979-576x1024&period;png" alt&equals;"" width&equals;"323" height&equals;"574" &sol;>&NewLine;

Exit mobile version