Saving Data on Android


Reading from external storage



Download 19,28 Mb.
Pdf ko'rish
bet32/292
Sana24.01.2022
Hajmi19,28 Mb.
#407089
1   ...   28   29   30   31   32   33   34   35   ...   292
Bog'liq
Bailey J., Dominguez A., Djermanovic D. - Saving Data on Android (1st Edition) - 2019

Reading from external storage
Replace the existing 
return
 statement of the 
getNote
 function with the following 
code:
val
 note = Note(fileName, 
""

// 1
 
if
 (isExternalStorageReadable()) { 
  
// 2
 
  FileInputStream(noteFile(fileName)).use { stream -> 
    
// 4
 
    
val
 text = stream.bufferedReader().use { 
      it.readText() 
    } 
    
// 5
 
    note.noteText = text 
  } 

return
 note
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
30


For the most part, the procedure here is the same as reading from the internal 
storage but with a small difference. Here’s how it works:
1. Ensure the external storage is readable.
2. Open and consume the 
FileInputStream
, with 
use
, as you did before.
3. Open a 
BufferedReader
 with 
use
 so that you can efficiently read the file.
4. Assign the text that was read to the file to 
note.noteText
 and return the 
note
.
The above code also relies on the following two functions:
fun
 
isExternalStorageWritable
()

Boolean
 { 
  
return
 Environment.getExternalStorageState() == 
Environment.MEDIA_MOUNTED 

 
fun
 
isExternalStorageReadable
()

Boolean
 { 
  
return
 Environment.getExternalStorageState() 
in
 
      setOf(Environment.MEDIA_MOUNTED, 
Environment.MEDIA_MOUNTED_READ_ONLY) 
}
The 
isExternalStorageWritable
 function determines if the external storage is 
mounted and ready for read/write operations, whereas 
isExternalStorageReadable
 determines only if the storage is ready for reading.
After writing and reading files, you’re ready to add the capability to delete a file from 
external storage.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   28   29   30   31   32   33   34   35   ...   292




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish