Bu dersimizde android dosya sisteminde internal storage bölümüne başlıyoruz. Bu derste internal storage’da dosya oluşturmayı ve kullanıcıdan aldığımız verileri bu dosyada saklamayı öğreneceğiz.
Internal Storage : Cihazın kendi iç hafıza birimidir. burada saklanılan dosyalar uygulamaya aittir. başka uygulamaların ve kullanıcının bu verilere ulaşması yasaktır. Kısıtlı olmasına rağmen sürekli erişilebilir durumdadır. Uygulama silindiğinde onun tüm verileri de silinir. Bu yüzden bu kısmı sadece uygulamanın kullanacağı veriler için kullanmalıyız.
Bu derste kullanılacak önemli methodları inceleyelim.
openFileOutput(String fileName, int mode): Bu method ismi verilen dosyayı verilen mod değerine göre açar, eğer o dosya yoksa oluşturur. Bu method geriye bir FileOutputStream döndürür. Bu döndürdüğü FileOutputStream nesnesi ile dosyamıza verileri yazar ve ilgili streamı yine bu nesne sayesinde kapatırız.
Mode olarak kullanacağımız MODE_PRIVATE : Oluşturulan dosyanın sadece o uygulamaya özel olduğunu belirtmeye yarar. MODE_APPEND bayrağı ise eğer belirtilen dosya zaten mevcutsa vereceğimiz veriyi dosyanın sonuna ekler, sil baştan yazmaz.
Dosyaya veriyi yazarken write methodunu kullanılır ve bu method verileri byte olarak yazar. Bu yüzden bu methoda veriyi verirken getBytes() methodundan faydalanırız.
Başlattığımız FileOutputStream’i close() methoduyla kapatmayı unutmamalıyız.
Ayrıca yeni activitiy’e manifest dosyasına belirtmeyi unutmayın.
Bu derste yazılan kodlar aşağıdaki gibidir. AndroidFileSystemOrnek projesinin bitmiş son halini buradaki linkten indirebilirsiniz.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void openInternalStorageDemoActivity(View view) { Intent intent=new Intent(this, InternalStorageDemo.class); startActivity(intent); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public class InternalStorageDemo extends AppCompatActivity { private EditText etFileName, etMessage, etFileToDelete; private TextView txvInternalStoragePath, txvFilesList; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.internal_storage); etFileName = (EditText) findViewById(R.id.etFileName); etMessage = (EditText) findViewById(R.id.etMessage); etFileToDelete = (EditText) findViewById(R.id.etFileToDelete); txvInternalStoragePath = (TextView) findViewById(R.id.txvInternalStoragePath); txvFilesList = (TextView) findViewById(R.id.txvFilesList); } public void saveToInternalStorage(View view) { String dosyaAdi=etFileName.getText().toString(); String data=etMessage.getText().toString(); FileOutputStream fos=null; try { fos=openFileOutput(dosyaAdi, MODE_PRIVATE); fos.write(data.getBytes()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(fos!=null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void showInternalStoragePath(View view) { } public void showFilesList(View view) { } public void deleteFile(View view) { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" tools:context="com.example.emrealtunbilek.androidfilesystemornek.InternalStorageDemo"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/etFileName" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="dosyaAdı" /> <EditText android:id="@+id/etMessage" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Mesaj" /> </LinearLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="saveToInternalStorage" android:text="Veriyi Dosyaya Kaydet" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="moveToDisplayScreen" android:text="Dosyaları Görüntüle" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="70dp" android:onClick="showInternalStoragePath" android:text="Internal Storage Dosya Yolu" /> <TextView android:id="@+id/txvInternalStoragePath" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="Internal Storage Yolu" android:textColor="@android:color/black" android:textSize="15sp" android:textStyle="italic" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:onClick="showFilesList" android:text="Internal Storagedaki Dosya Listesi" /> <TextView android:id="@+id/txvFilesList" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:text="Dosyaların Adları" android:textColor="@android:color/black" android:textSize="15sp" android:textStyle="italic" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:orientation="horizontal"> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:onClick="deleteFile" android:text="Dosyayı Sil" /> <EditText android:id="@+id/etFileToDelete" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Silinecek Dosya Adı" /> </LinearLayout> </LinearLayout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" tools:context="com.example.emrealtunbilek.androidfilesystemornek.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Internal Storage Demo" android:onClick="openInternalStorageDemoActivity"/> </LinearLayout> |