0% found this document useful (0 votes)
38 views4 pages

Mad Unit 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
38 views4 pages

Mad Unit 4

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 4
A Persistent i yt RT. PART-B Essay QUESTIONS WITH SOLUTIONS i! USING APPLICATION SPECIFIC FOLDERS AND FILES o pofine file. Discuss the usage of application ‘Specific folders and files, yer? ae! ‘Model Papers, a3 jie Alea collection of records where each record od provides an nfrmai pessoa Sa deve and cn beaccesed hough set oft ton MES eel co Most ofthe applications create o dowload the ies that 0 ° fe specific to application. The aplication speci ook inemaly een. Whierering exer sag te tardondeamopatohanee ee pins and canbe mounted to computer file sytem when device is conse trouphUSB Brennen Insvacvey {eter deviees wll mplerent it 5 a sparte parton atemal storage. in ‘Security fsnot imposed on th les which ae stored on extemal storage, These files anbe accesed,overoitenor deleted tyanyapplication. The files stored on external: storage might not always be available, The application jcannot read files on external squoge when the SD cari jected or devieis mounted through computer, = «Android provides two methods through application context. They aes follows, 1 getDin() ‘This method is used to retum the file path that as path othe intemal application file storage dtectory. 2. getExternalFilesDie() ‘Tis method is used to rtur the file path that has path to the external application ile storage directory. ‘The getExternalFilesDir method was introduced in Android APL. The Environment geExtemalStorageDiretory() must te called 0 retum a path o root of extemal storage, , ‘The methods getDirand getEstemalflesDir accept Sung aguneatt spect subdietory ia which tefl needa be stored The files that are stored in application folders must be specific to parent application and are even not detected by media- scanner, Thus, they cannot be added to Media Library automatically. If ile are created or application is downloaded that need tobe added to Media Library orto be made available to other applications then move them to publi extemal storage directo 4.2. CREATING FILES, READING DATA FROM FILES, LISTING CONTENTS OF A DIRECTORY 10. Write short notes on the following, () Creating files (ii) Reading and writing data from and to files. Answer ¢ Mote Papers, 8 (Creating Fes In android fle canbe created by using openFleOurput() method. Syntax Sting FILE_NAME = "ile amp"; FiteOurputS ream os = openfileOutpu(FILE_ NAME, context MODE, PRIVATE): FilelnputSteam is = openFilelnput (FILE NAME); ‘SPECTRUM ALLIN-ONE JOURNAL FOR ENGINEERING STUDENTS ‘Scanned with CamScanner | Ri 46 MOBILE APPLICATION DEVELOPMENT (JNTU HYDERABAD) “The methods used in above code support the ile of present applieaton folder. Speifyng pat separators makes exe tin visting files isthe deft behaviour of them In order to append data to the existing file, the mode must be spent Conlon fies that are created by using openleOutput method are private by default the cling op ion, The file ean be shared in between application by using the content provider, The other metho is to sPecily com NODE WORLD READABLE orcontext MODE WORLD-WRITADLE whl erating output file in oderto make avaisble tote applications. String outputfile = ‘publicxt; FileOutputStream os = openFileOutpul(OUTPUT_FILE context MODE_WORLD_WRITABLE);, The location of files stored in sandbox can be known by calling getFilesDi. The method returns absolute path to fies hich are created by using openFileOutput, Syntax File f= getFilesDin(); Log.d("OUTPUT_PATH." ile getAbsolutesPath( ); Reading and Writing Data from and to Files In android, the data can be read and' written by using openfilelnput{) and openFileOutput( ) respectively. The method ‘openFileOutput() i used to create and save a file. After this data can be writen to the file. Whereas, the method openFilelnpa ) ‘method is used 1 open a file for reading, It returns an instance of FileInputStream. _ FilelnputStream is = openFilelnput(ile); Example A simple example to read one character from a fie i as given below, intx; String tmp =" while((x=isread())! =- 1) { ‘tmp = tmp + Character toString ((char)x);, } é: isclose(); _ /Avhete ‘sis the instance of FilelnputStream, Q11. How contents of a directory are listed? . Answer + : re 1A as Mode Papert ot ‘Thefiles or contents ofa drectory canbe displayedby wing" command, The ls command writs the comes ofspecifd ivetory ox name of specif ile tothe standard output. [ists the content of present directory if patil le or directory isnot specified. It displays all the contents in alphabetical order of filename, ‘The ls Command with Options ‘The various options of ‘Is! command are as follows, Description displays all the contents: ‘excluding the hidden folders or files (ie., dot(,) files and dot dot.) [displays all the files along with the hidden files Te displays one ile per line It displays the files in the reverse order Ttisplays the files as a comma-separated list displays only directories Te displays the files based on the time modified files) eee ‘Scanned with CamScanner stent Storage character of evry ety might be distor), Hock special le, character special le), Kaymbo wt pie special file), (local socket) ete. The next nine characters are divided into three sets. The fis set depicts creat emision the second set depicts the permission of remaining users in a group. And, the last set n of others who can access the file. The characters ofeach ofthese sets represent the read (1), write (w) and s of file. The detailed information can be displayed by using the below command, Is-1filel profile {pe deed information ofa directory canbe displayed by usiig below command, 15-d-Lmanisal manualfilel SHARED PREFERENCES - CREATING SHARED PREFERENCES, SAVING AND RETRIEVING DATA USING SHARED PREFERENCE oie. What are shared preferences? How are they created? * seower (od ape, Gods Pape 2) cgared Preferences Shared preferences are used to store the data of various primitive applications using their easy, ight weight NVP (name! pe) mechanism They are required when the usr want to store Ul state ser preferences or aplication setigs, They cers to save sets of name/value pairs of primitive data as named preferences. The various primitive types supported Sow {cred preferences include boolean, tring, oa, long and integer by making them ideal means to store current UI ste ser feroces, default values and class instance variables (Creating Shared Preferences ‘A shared preference canbe created by calling geSharedPreferences() on buen context bypassing meas anargumest- ‘ShazedPreferences spl = getSharedPreferences (PREF I, Activity. MODE_PRIVATE); shared preference canbe modified by using Sharedrefeences Editor cass. The editor object canbe rated by ling fs) on shared preferences object tha isto be modified. ‘SharedPreferences Editor ed) = spl.eit(); “The potcaype> methods are used fr inserting or updating he values accompanied with the given name, Examples editor puBoolear( "I" editor paPloaP, 19 editorputin("€", 2), editorptLong("T" 26% aoe patting textEntryval’, "Not Empty"), 13. Explain how shared preferences are used to save and retrieve the data. ruc) Answer: Model Papert, a9. Saving Shared Preferences “rhe ehared preferences are saved in sandbox of te application. Ths, they canbe easily shared Py comPonents of the spplictions, Note t,he shared preferences of one ppt anna be acesel by oer applications. The various modifications done on he er objet canbe saved by sing the aply or commit nehes Nee the apply() vetted at save the modifiations asynchronously and consi) method is used to save the moiicsions synchronously. Syatax eAlapply(); saving modiiations asynchronously eAl.commit(j; saving modifications synchronously “The appy() method most prefered method to sve shared preferences sae itis asynchronous The ccommtit() method a naa asus required. This method blocks the calling ead and returns tein case of sucessful te otherwise false is returned, SPECTRUM ALL-IN-ONE JOURNAL FOR ENGINEERING STUDENTS X u ‘Scanned with CamScanner 4.8 MOBILE APPLICATION DEVELOPMENT [JNTU-HYCERABAD) ing Shared Preferences The shared preferences can be retrieved or accessed by using getSharedPrcference retrieved by using type-safe get method. Every getter accepts a key and default value as Syntax sharedPreferences sp = getSharedPreferences("Settings", contéxt MODE_PRIVATE); Now, this can be accessed by using the below statement, String settings = SharedPreferences,getString(*KeyName'",“defaultValue"); boolean bl = mySharedPreferences.getBoolean ("bl fils); float f= mySharedPreferences,getFloat("t", 01); int d= mySharedPreferences.getint(“d", 1); ong | = mySharedPreferences.getLong("t", String sp = mySharedPreferences.getString( xtEntryVal = uments. ) method. The saved values can he A inap of the available shared preferences key values ear abtained by calling getANN() method and the availability of a specific key can be determined by calling containa( ) method. Syntax. Map allPreferences = mySharedPreferences.getAll( ); boolean clf= mySharedPreferences.contain("It"); SHORT QUESTIONS Q1. Whatis persistent storage? Ans: For answer refer Unit-IV, Ql. Q2, Define shared preferences. Important Question Ans: For answer refer Unit-IV, 5 tmnportan Question Q3. Write about creating shared preferences. i Ans: For answer refer Unit-IV, Q6. ienportant enon ESSAY QUESTIONS ‘ ~ Q4. Define file. Discuss the usage of application specific ‘~Iders and files. Ans: For answer refer Unit-V, Q9. : important aeson Q5. Write short notes on the following, (i) Creating files r (ii) Reading and writing data from and to files. o Ans: For answer refer Unit-IV, QUO. . © enporant ue Q6. How contents of a directory are listed? : Ans: For answer refer Unit-lV, QUL Important 5 Q7. Explain how shared preferences are used to save and retrieve the data. vapor oo Ans: For answer refer Unit-IV, QL3. WARNING: xerowPhotocopying of his books CRIMINAL ct Anyone found gui is LIABLE to faghKEGAL process ‘Scanned with CamScanner on

You might also like