Saving Data on Android



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

WRITE_EXTERNAL_STORAGE
 permission inside the 

 element in 
AndroidManifest.xml
.
Wri/ng the notes to external storage
Now that the correct permissions are in place, it’s time to write the file to the 
external storage. Open 
ExternalFileRepository.kt
 and add the following code to 
addNote
:
Saving Data on Android
Chapter 1: Using Files
raywenderlich.com
29


// 1
 
if
 (isExternalStorageWritable()) { 
  
// 2
 
  FileOutputStream(noteFile(note.fileName)).use { output -> 
    output.write(note.noteText.toByteArray()) 
  } 
}
Here’s how it works:
1. Check to see if the external storage is available.
2. Open a 
FileOutputStream
 with 
use
.
3. Write 
note.noteText
 to the file.
Next, in 
MainActivity.kt
, change the instance of the 
NoteRepository
 you’re 
initializing to the following:
private
 val repo: 
NoteRepository
 by 
lazy
 

ExternalFileRepository
(this) }
Finally, run the program to write a file to the external storage. To view the file using 
the Device File Explorer, look in 
sdcard/data
, within the app’s package name folder.
You’ll implement the app’s read function in the next step.

Download 19,28 Mb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   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