SlideShare a Scribd company logo
STORING LOCAL DATA IN
ANDROID
DHARSHINI B
20CS1H4204
INTRODUCTION
• Storing local data in an Android application involves saving
data on the device's local storage, which can be accessed
and manipulated by the application. Local data can be any
type of data, such as user preferences, settings, cached data,
media files, and more.
WAYS TO STORE LOCAL DATA IN ANDROID
• Shared Preferences
• Internal storage
• External storage
• SQLite databases
Internal storage
In Android, you can store data in internal storage using the file system APIs.
Internal storage is private to your app and is not accessible to other apps or
users. Here are the steps to store data in internal storage:
• Get a reference to the internal storage directory for your app using the
getFilesDir() method:
File directory = context.getFilesDir();
• Create a File object to represent the file you want to store:
File file = new File(directory, "myfile.txt");
• Write data to the file using a FileOutputStream. You can use a
BufferedWriter to write strings to the file:
try {
FileOutputStream fos = new FileOutputStream(file);
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(fos));
writer.write("Hello world!");
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
• To read data from the file, create a FileInputStream and read the contents into a
byte array. You can use a BufferedReader to read strings from the file:
try {
FileInputStream fis = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = reader.readLine();
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
EXTERNAL STORAGE
In Android, you can store local data in an external file using the file
system APIs. Here are the steps you can follow:
• Add the WRITE_EXTERNAL_STORAGE permission to your app's
AndroidManifest.xml file. This permission allows your app to write to
external storage.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
• Check for the permission at runtime, since devices running Android
6.0 or later require the user to grant permission at runtime.
if (ContextCompat.checkSelfPermission(context,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
• Create a File object representing the file to write to. You can use the
Environment.getExternalStorageDirectory() method to get the root
directory of the external storage. Then, append the file path to it.
File file = new File(Environment.getExternalStorageDirectory(),
"file.txt");
• Write data to the file using a FileOutputStream.
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
• To read data from the file, create a FileInputStream and read the contents
into a byte array.
try {
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
String content = new String(data, "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
THANK YOU

More Related Content

PPTX
Internal Storage in Mobile Application Development.pptx
DOCX
Android-data storage in android-chapter21
PPTX
6CS027 Lecture 5 Files and Database Storage (1).pptx
PPT
Data Storage In Android
PDF
Lab4 - android
PDF
Android datastorage
PPTX
Lecture 8: Android Types of Storage.pptx
PPTX
Android session 4-behestee
Internal Storage in Mobile Application Development.pptx
Android-data storage in android-chapter21
6CS027 Lecture 5 Files and Database Storage (1).pptx
Data Storage In Android
Lab4 - android
Android datastorage
Lecture 8: Android Types of Storage.pptx
Android session 4-behestee

Similar to MA DHARSH.pptx (20)

PPTX
Third-party App Stores on Android.pptx.pdf
PPTX
Android Data Storagefinal
PDF
03 programmation mobile - android - (stockage, multithreads, web services)
PDF
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
ODP
Android App Development - 09 Storage
PDF
Mobile Application Development -Lecture 11 & 12.pdf
PDF
Android - Saving data
PPTX
Storage 8
PDF
Windows phone 8 session 8
PDF
Mobile Application Development-Lecture 13 & 14.pdf
PPTX
Assets, files, and data parsing
PDF
Everything about storage - DroidconMtl 2015
PDF
Android local databases
PPT
Memory management
PPTX
Mobile application Development-UNIT-V (1).pptx
PPTX
Android Training (Storing & Shared Preferences)
PPT
Unit 2 in environment science and technology
PPT
iOS Application Pentesting
PPT
"Android" mobilių programėlių kūrimo įvadas #3
DOCX
Android media-chapter 23
Third-party App Stores on Android.pptx.pdf
Android Data Storagefinal
03 programmation mobile - android - (stockage, multithreads, web services)
Pertemuan 13 - Shared Preferences and Settings - Word - Salin.pdf
Android App Development - 09 Storage
Mobile Application Development -Lecture 11 & 12.pdf
Android - Saving data
Storage 8
Windows phone 8 session 8
Mobile Application Development-Lecture 13 & 14.pdf
Assets, files, and data parsing
Everything about storage - DroidconMtl 2015
Android local databases
Memory management
Mobile application Development-UNIT-V (1).pptx
Android Training (Storing & Shared Preferences)
Unit 2 in environment science and technology
iOS Application Pentesting
"Android" mobilių programėlių kūrimo įvadas #3
Android media-chapter 23

Recently uploaded (20)

PPTX
Logistic Regression ml machine learning.pptx
PPTX
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
PPTX
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
PPTX
lec_5(probability).pptxzzjsjsjsjsjsjjsjjssj
PPT
Reliability_Chapter_ presentation 1221.5784
PDF
Company Presentation pada Perusahaan ADB.pdf
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PDF
Report The-State-of-AIOps 20232032 3.pdf
PDF
Chad Readey - An Independent Thinker
PPTX
Moving the Public Sector (Government) to a Digital Adoption
PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Linux OS guide to know, operate. Linux Filesystem, command, users and system
PPTX
Trading Procedures (1).pptxcffcdddxxddsss
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
Global journeys: estimating international migration
PDF
Master Databricks SQL with AccentFuture – The Future of Data Warehousing
PPTX
Computer network topology notes for revision
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
PPTX
Business Acumen Training GuidePresentation.pptx
Logistic Regression ml machine learning.pptx
Data-Driven-Credit-Card-Launch-A-Wells-Fargo-Case-Study.pptx
DISORDERS OF THE LIVER, GALLBLADDER AND PANCREASE (1).pptx
lec_5(probability).pptxzzjsjsjsjsjsjjsjjssj
Reliability_Chapter_ presentation 1221.5784
Company Presentation pada Perusahaan ADB.pdf
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Report The-State-of-AIOps 20232032 3.pdf
Chad Readey - An Independent Thinker
Moving the Public Sector (Government) to a Digital Adoption
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Linux OS guide to know, operate. Linux Filesystem, command, users and system
Trading Procedures (1).pptxcffcdddxxddsss
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
Miokarditis (Inflamasi pada Otot Jantung)
Global journeys: estimating international migration
Master Databricks SQL with AccentFuture – The Future of Data Warehousing
Computer network topology notes for revision
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
Business Acumen Training GuidePresentation.pptx

MA DHARSH.pptx

  • 1. STORING LOCAL DATA IN ANDROID DHARSHINI B 20CS1H4204
  • 2. INTRODUCTION • Storing local data in an Android application involves saving data on the device's local storage, which can be accessed and manipulated by the application. Local data can be any type of data, such as user preferences, settings, cached data, media files, and more.
  • 3. WAYS TO STORE LOCAL DATA IN ANDROID • Shared Preferences • Internal storage • External storage • SQLite databases
  • 4. Internal storage In Android, you can store data in internal storage using the file system APIs. Internal storage is private to your app and is not accessible to other apps or users. Here are the steps to store data in internal storage: • Get a reference to the internal storage directory for your app using the getFilesDir() method: File directory = context.getFilesDir(); • Create a File object to represent the file you want to store: File file = new File(directory, "myfile.txt");
  • 5. • Write data to the file using a FileOutputStream. You can use a BufferedWriter to write strings to the file: try { FileOutputStream fos = new FileOutputStream(file); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos)); writer.write("Hello world!"); writer.close(); } catch (IOException e) { e.printStackTrace(); }
  • 6. • To read data from the file, create a FileInputStream and read the contents into a byte array. You can use a BufferedReader to read strings from the file: try { FileInputStream fis = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); String line = reader.readLine(); reader.close(); } catch (IOException e) { e.printStackTrace(); }
  • 7. EXTERNAL STORAGE In Android, you can store local data in an external file using the file system APIs. Here are the steps you can follow: • Add the WRITE_EXTERNAL_STORAGE permission to your app's AndroidManifest.xml file. This permission allows your app to write to external storage. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 8. • Check for the permission at runtime, since devices running Android 6.0 or later require the user to grant permission at runtime. if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE); }
  • 9. • Create a File object representing the file to write to. You can use the Environment.getExternalStorageDirectory() method to get the root directory of the external storage. Then, append the file path to it. File file = new File(Environment.getExternalStorageDirectory(), "file.txt");
  • 10. • Write data to the file using a FileOutputStream. try { FileOutputStream fos = new FileOutputStream(file); fos.write(data.getBytes()); fos.close(); } catch (IOException e) { e.printStackTrace(); }
  • 11. • To read data from the file, create a FileInputStream and read the contents into a byte array. try { FileInputStream fis = new FileInputStream(file); byte[] data = new byte[(int) file.length()]; fis.read(data); fis.close(); String content = new String(data, "UTF-8"); } catch (IOException e) { e.printStackTrace(); }