با عرض سلام و وقت بخیر خدمت کاربران سایت پی وی لرن . و کاربرانی که دوره کامل آموزش برنامه نویسی اندروید را دنبال می کنند. آندروید انواع مختلف ذخیره سازی را برای برنامه های کاربردی فراهم می کند تا قابلیت ذخیره سازی داده ها فراهم شود. این مکان های ذخیره سازی، shared preferences، ذخیره سازی داخلی و خارجی، ذخیره سازی در SQLite و ذخیره سازی از طریق اتصال به شبکه هستند. در این بخش ما به ذخیره سازی داخلی (Internal storage) نگاهی خواهیم داشت. ذخیره سازی داخلی ذخیره کردن داده های دریافتی در اپلیکیشن در حافظه دستگاه است. درحالت پیش فرض فایل هایی که با این روش ذخیره می شوند خصوصی بوده و فقط از طریق اپلیکیشن مربوطه قابل دسترسی هستند. در ادامه به آشنایی با حافظه داخلی در اندروید می پردازیم.
در این مبحث به چگونگی کار با حافظه داخلی در برنامه نویسی اندروید جهت ذخیره سازی فایل می پردازیم.
برای استفاده از حافظه داخلی برای نوشتن برخی داده ها در فایل، باید متد () openFileOutput استفاده کنید.
برای استفاده از متد () openFileOutput باید نام و نوع فایل نیز ذکر شود.
ساختار استفاده از متد فوق به صورت زیر است:
1 | FileOutputStream fOut = openFileOutput("file name here",MODE_WORLD_READABLE); |
متد () openFileOutput نمونه ای از FileOutputStream را باز می گرداند. بنابراین شما آن را در شی FileInputStream دریافت می کنید.
سپس می توانید داده ها را در فایل مورد نظر با ساختار زیر بنویسید:
1 2 3 | String str = "data"; fOut.write(str.getBytes()); fOut.close(); |
استفاده از متد () openFileInput با نام فایل. یک instance از FileInputStream را برای خواندن فایل، باز می گرداند.
ساختار آن به صورت زیر است:
1 | FileInputStream fin = openFileInput(file); |
سپس می توانید از متد read برای خواندن تک تک کاراکترهای ذخیره شده در فایل استفاده کنید:
1 2 3 4 5 6 | int c; String temp=""; while( (c = fin.read()) != -1){ temp = temp + Character.toString((char)c); } fin.close(); |
علاوه بر متدهای write و close متدهای دیگری نیز برای نوشتن در فایل توسط کلاس FileOutputStream ارائه شده که به شرح زیر است:
ردیف | متد و توضیحات مربوطه |
---|---|
۱ | FileOutputStream(File file, boolean append) یک FileOutputStream جدید را برای نوشتن فایل ایجاد می کند. |
۲ | getChannel() این متد یک FileChannel فقط قابل خواندن از فایل برمی گرداند. |
۳ | getFD() این متد توصیف زمینه ی فایل را باز می کند. |
۴ | write(byte[] buffer, int byteOffset, int byteCount) این متد تعداد بایت ها را از آرایه ی بافر بایت می نویسد. |
علاوه بر متدهای read و close متدهای دیگری نیز برای نوشتن در فایل توسط کلاس FileInputStream ارائه شده که به شرح زیر است:
ردیف | متد و توضیحات مربوطه |
---|---|
۱ | available() این متد یک مقدار تخمینی از بایت های ذخیره شد را برای جلوگیری از ورود داده های اضافی برمی گرداند. |
۲ | getChannel() این متد یک FileChannel فقط قابل خواندن از فایل برمی گرداند. |
۳ | getFD() این متد توصیف زمینه ی فایل را باز می کند. |
۴ | read(byte[] buffer, int byteOffset, int byteCount) این متد بایت های طول این stream را می خواند و آنها را در آرایه b شروع می کند که از صفر شروع می شود. |
در ادامه به یک مثال ساده از ایجاد یک برنامه برای ذخیره سازی و خواندن داده ها در حافظه ی داخلی دستگاه می پردازیم.
این برنامه داده ها را در قالب فایل در حافظه ی دستگاه بصورت خصوصی ذخیره می کند.
شما می توانید این برنامه را در یک دستگاه اندروید واقعی یا شبیه ساز اندروید اجرا کنید.
مراحل انجام کار:
مراحل | توضیحات |
---|---|
۱ | مراحل ایجاد برنامه در اندروید استدیو را طبق بخش های قبلی دنبال کرده و نام پکیج را com.pvlearn.myapplication قرار بدهید. |
۲ | به محتوای فایل src/MainActivity.java کدهای مورد نیاز را اضافه کنید. |
۳ | طراحی مناسب این برنامه را در فایل res/layout/activity_main ایجاد کنید. |
۴ | برنامه را اجرا کنید تا شبیه ساز اندروید نتیجه را نمایش دهد. |
محتوای فایل src/MainActivity.java:
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 | package com.example.sairamkrishna.myapplication; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; import java.io.FileInputStream; import java.io.FileOutputStream; public class MainActivity extends Activity { Button b1,b2; TextView tv; EditText ed1; String data; private String file = "mydata"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button); b2=(Button)findViewById(R.id.button2); ed1=(EditText)findViewById(R.id.editText); tv=(TextView)findViewById(R.id.textView2); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { data=ed1.getText().toString(); try { FileOutputStream fOut = openFileOutput(file,MODE_WORLD_READABLE); fOut.write(data.getBytes()); fOut.close(); Toast.makeText(getBaseContext(),"file saved",Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { try { FileInputStream fin = openFileInput(file); int c; String temp=""; while( (c = fin.read()) != -1){ temp = temp + Character.toString((char)c); } tv.setText(temp); Toast.makeText(getBaseContext(),"file read",Toast.LENGTH_SHORT).show(); } catch(Exception e){ } } }); } } |
محتوای فایل res/layout/activity_main.xml:
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 | <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:text="Internal storage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textview" android:textSize="35dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials point" android:id="@+id/textView" android:layout_below="@+id/textview" android:layout_centerHorizontal="true" android:textColor="#ff7aff24" android:textSize="35dp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" android:id="@+id/button" android:layout_alignParentBottom="true" android:layout_alignLeft="@+id/textView" android:layout_alignStart="@+id/textView" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:hint="Enter Text" android:focusable="true" android:textColorHighlight="#ff7eff15" android:textColorHint="#ffff25e6" android:layout_below="@+id/imageView" android:layout_alignRight="@+id/textView" android:layout_alignEnd="@+id/textView" android:layout_marginTop="42dp" android:layout_alignLeft="@+id/imageView" android:layout_alignStart="@+id/imageView" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView" android:src="@drawable/abc" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="load" android:id="@+id/button2" android:layout_alignTop="@+id/button" android:layout_alignRight="@+id/editText" android:layout_alignEnd="@+id/editText" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Read" android:id="@+id/textView2" android:layout_below="@+id/editText" android:layout_toLeftOf="@+id/button2" android:layout_toStartOf="@+id/button2" android:textColor="#ff5bff1f" android:textSize="25dp" /> </RelativeLayout> |
محتوای فایل res/values/string.xml:
1 2 3 | <resources> <string name="app_name">My Application</string> </resources> |
محتوای فایل AndroidManifest.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
برای اجرای برنامه ی اندرویدی خود یکی از فایل های activity در برنامه ی خود کلیک کنید.
سپس روی نماد (Run) از نوار ابزار برای شروع اجرای برنامه کلیک کنید.
سپس نتیجه اجرای برنامه را در دستگاه اندروید خود مشابه زیر مشاهده خواهید کرد:
حالا باید متن مورد نظر را در کادر text مربوطه وارد کنید.
سپس روی دکمه ی save کلیک کرده تا نتیجه را همانند تصویر زیر مشاهده کنید:
سپس روی load کلیک کنید تا اپلیکیشن داده ها را از فایل بخواند.
سپس داده ها را در صفحه برنامه به صورت زیر نمایش می دهد:
همچنین می تواند با مراجعه به زبانه ی DDMS فایل ذخیره شده در دستگاه را مشاهده کنید.
پس از مراجعه به زبانه ی DDMS ابزار file explorer را انتخاب کرده و به مسیر زیر مراجعه کنید:
1 | tools>android>android device Monitor |
اقدام فوق پنجره ای مشابه پنجره ی زیر را نمایش خواهد داد:
اندروید به شما اجازه می دهد تا داده های متنی خود را در قالب فایل در حافظه ی داخلی خود به صورت خصوصی ذخیره کرده و در صورت لزوم داده های آن را خوانده و نمایش دهید، از این رو مباحث حافظه داخلی در برنامه نویسی اندروید برای آشنایی شما با این تکنیک ارائه شد.
در بخش بعدی نیز به مباخث کار با JetPlayer در برنامه نویسی اندروید می پردازیم.