1 of 11

Internal va External Storage�Fayl yaratish, o‘qish/yozish, permissionlar bilan ishlash�

2 of 11

Android’da fayllar bilan ishlash:�

  • Internal Storage
  • External Storage
  • Fayl yaratish
  • Faylga yozish va o‘qish
  • Permissionlar (runtime permissions)

3 of 11

1. Internal Storage (Ichki xotira)�

  • Asosiy xususiyatlar:
  • Faqat shu app foydalanadi (private).
  • Ilova o‘chirilganda fayllar ham o‘chadi.
  • Maxfiy ma’lumotlar uchun xavfsiz.
  • Permission talab qilmaydi.
  • Internal Storage fayl yo‘li:

/data/data/<app-package>/files/

4 of 11

Foydalanish:�

FileOutputStream fos = openFileOutput("data.txt", MODE_PRIVATE); fos.write("Hello Internal Storage".getBytes());

fos.close();

O‘qish:

FileInputStream fis = openFileInput("data.txt");

5 of 11

External Storage (Tashqi xotira)�

Asosiy xususiyatlar:

  • Tashqi xotira (SDcard, Memory shared area)
  • Boshqa ilovalar ham ko‘ra oladi.
  • Permission talab qiladi:
    • READ_EXTERNAL_STORAGE
    • WRITE_EXTERNAL_STORAGE
  • Katta fayllar uchun qulay (rasmlar, videolar, loglar).

External Storage yo‘llari:

  • Public directory�(Pictures, Downloads, Documents)
  • App-specific directory�(Android/data/<package>/files/)

6 of 11

Android 10+ (API 29) dan boshlab: Scoped Storage

O‘zgarishlar:

  • App boshqa papkalarga kira olmaydi.
  • Public media fayllar MediaStore orqali boshqariladi.
  • Yozish uchun maxsus permissionlar talab qilinadi.

Scoped Storage sababli eski WRITE_EXTERNAL_STORAGE ruxsati ko‘p holatda ishlatilmaydi.

7 of 11

4. Runtime Permissions (Android 6+)�Foydalanuvchi permissionni run-time da tasdiqlashi kerak.�Permission so‘rash (modern API):

if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)

!= PackageManager.PERMISSION_GRANTED) {

requestPermissions(

new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},

100

);

}

8 of 11

Amaliy misollar:

  • A) INTERNAL STORAGE: Fayl yaratish, yozish, o‘qish

public void writeInternal(String text) {

try {

FileOutputStream fos = openFileOutput("mytext.txt", MODE_PRIVATE);

fos.write(text.getBytes());

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

9 of 11

2. O‘qish:

public String readInternal() {

StringBuilder sb = new StringBuilder();

try {

FileInputStream fis = openFileInput("mytext.txt");

int ch;

while ((ch = fis.read()) != -1) {

sb.append((char) ch);

}

fis.close();

} catch (Exception e) {

e.printStackTrace();

}

return sb.toString();

}

10 of 11

B) EXTERNAL STORAGE: Fayl yaratish, yozish, o‘qish�1. Permission qo‘shish (AndroidManifest.xml)�

  • <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  • <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

11 of 11

2. Yozish (App-specific directory)

public void writeExternal(String text) {

File path = getExternalFilesDir(null); // app folder

File file = new File(path, "notes.txt");

try {

FileOutputStream fos = new FileOutputStream(file);

fos.write(text.getBytes());

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}