Android存儲(chǔ)卡讀寫文件與Application數(shù)據(jù)保存的實(shí)現(xiàn)介紹
一、在存儲(chǔ)卡上讀寫圖片文件
Android的位圖工具是Bitmap,App讀寫B(tài)itmap可以使用性能更好的BufferedOutputStream和BufferedInputStream。
Android還提供了BitmapFactory工具用于讀取各種來源的圖片,相關(guān)方法如下:
- decodeResource:該方法可從資源文件中讀取圖片信息。
- decodeFile:該方法可將指定路徑的圖片讀取到Bitmap對(duì)象。
- decodeStream:該方法從輸入流中讀取位圖數(shù)據(jù)。
例:點(diǎn)擊保存再點(diǎn)擊讀取后出現(xiàn)圖片
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存"/> <Button android:id="@+id/btn_read" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="讀取"/> <ImageView android:id="@+id/iv_content" android:layout_width="match_parent" android:layout_height="400dp" android:scaleType="fitCenter"/> </LinearLayout>
java代碼
public class ImageWriteActivity extends AppCompatActivity implements View.OnClickListener { private ImageView iv_content; private String path; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_image_write); iv_content = findViewById(R.id.iv_content); findViewById(R.id.btn_save).setOnClickListener(this); findViewById(R.id.btn_read).setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.btn_save: String fileName = System.currentTimeMillis()+".jpeg"; // 獲取當(dāng)前APP的私有下載目錄 path = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString()+ File.separatorChar+fileName; // 從指定的資源文件中獲取位圖對(duì)象 Bitmap b1 = BitmapFactory.decodeResource(getResources(),R.drawable.thing1); // 把位圖對(duì)象保存為圖片文件 FileUtil.saveImage(path,b1); Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); break; case R.id.btn_read: Bitmap b2 = FileUtil.openImage(path); iv_content.setImageBitmap(b2); break; } } }
FileUtil類
public class FileUtil { // 把位圖數(shù)據(jù)保存到指定路徑的圖片文件 public static void saveImage(String path, Bitmap bitmap) { FileOutputStream fos = null; try { fos = new FileOutputStream(path); // 把文件數(shù)據(jù)壓縮到文件輸出流 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } //從指定路徑的圖片文件中讀取位圖數(shù)據(jù) public static Bitmap openImage(String path) { Bitmap bitmap = null; FileInputStream fis = null; try { fis = new FileInputStream(path); bitmap = BitmapFactory.decodeStream(fis); } catch (Exception e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmap; } }
二、Application
1、Application生命周期
Application是Android的一大組件,在App運(yùn)行過程中有且僅有一個(gè)Application對(duì)象貫穿整個(gè)生命周期。
清單文件AndroidMainifest.xml文件中有個(gè)application標(biāo)簽,沒有指定則使用默認(rèn);
可以新建一個(gè)MyApplication類繼承Application,然后在清單文件中加入以下代碼指定使用自定義的application。
android:name=".MyApplication"
MyApplication類
public class MyApplication extends Application { //App啟動(dòng)時(shí)調(diào)用 public void onCreate() { super.onCreate(); mApp = this; } //App終止調(diào)用 @Override public void onTerminate() { super.onTerminate(); } //App配置改變調(diào)用,如豎屏變?yōu)闄M屏 @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); } }
2、利用Application操作全局變量
全局的意思是其他代碼都可以引用該變量,因此全局變量是共享數(shù)據(jù)和消息傳遞的好方法。
適合在Application中保存的全局變量主要有:
- 會(huì)頻繁讀取的信息,如用戶名、手機(jī)號(hào)等。
- 不方便由意圖傳遞的數(shù)據(jù),如位圖對(duì)象、非字符串類型的集合對(duì)象等。
- 容易因頻繁分配內(nèi)容而導(dǎo)致內(nèi)存泄漏的對(duì)象,如Handler對(duì)象等。
例:輸入信息點(diǎn)擊保存,只要應(yīng)用不掛掉,重新回到應(yīng)用還會(huì)有輸入的信息。
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="5dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal"> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="姓名" android:textSize="17sp"/> <EditText android:id="@+id/et_name" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:hint="請(qǐng)輸入姓名" android:inputType="text" android:textSize="17sp"/> </LinearLayout> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存" android:textSize="17sp"/> </LinearLayout>
java類
public class AppWriteActivity extends AppCompatActivity implements View.OnClickListener { private EditText et_name; private MyApplication app; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_app_write); et_name = findViewById(R.id.et_name); app = MyApplication.getInstance(); findViewById(R.id.btn_save).setOnClickListener(this); reload(); } private void reload() { String name = app.infoMap.get("name"); if(name == null) return; et_name.setText(name); } @Override public void onClick(View view) { String name = et_name.getText().toString(); app.infoMap.put("name",name); } }
MyApplication類
public class MyApplication extends Application { private static MyApplication mApp; // 聲明一個(gè)公共的信息映射對(duì)象,可當(dāng)作全局變量使用 public HashMap<String,String> infoMap = new HashMap<>(); public static MyApplication getInstance(){ return mApp; } //App啟動(dòng)時(shí)調(diào)用 public void onCreate() { super.onCreate(); mApp = this; } //App終止調(diào)用 @Override public void onTerminate() { super.onTerminate(); } //App配置改變調(diào)用,如豎屏變?yōu)闄M屏 @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); } }
到此這篇關(guān)于Android存儲(chǔ)卡讀寫文件與Application數(shù)據(jù)保存的實(shí)現(xiàn)介紹的文章就介紹到這了,更多相關(guān)Android存儲(chǔ)卡讀寫文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android應(yīng)用中ListView利用OnScrollListener分頁加載數(shù)據(jù)
這篇文章主要介紹了Android應(yīng)用中ListView利用OnScrollListener分頁加載數(shù)據(jù)的方法,包括對(duì)OnScrollListener事件順序次數(shù)的分析,需要的朋友可以參考下2016-03-03Kotlin 使用高階函數(shù)實(shí)現(xiàn)回調(diào)方式
這篇文章主要介紹了Kotlin 使用高階函數(shù)實(shí)現(xiàn)回調(diào)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android開發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能【附源碼下載】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)可拖動(dòng)排序的ListView功能,結(jié)合實(shí)例形式分析了Android列表拖動(dòng)排序原理與相關(guān)操作技巧,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11kotlin實(shí)現(xiàn)通知欄提醒功能示例代碼
這篇文章主要給大家介紹了關(guān)于kotlin實(shí)現(xiàn)通知欄提醒功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用kotlin具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08Android Rreact Native 常見錯(cuò)誤總結(jié)
這篇文章主要介紹了Android Rreact Native 常見錯(cuò)誤總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-06-06Flutter實(shí)現(xiàn)不同縮放動(dòng)畫效果詳解
這篇文章主要為大家詳細(xì)介紹了Flutter利用不同組件(ScaleTransition、SizeTransition、AnimatedSize和AnimatedBuilder)實(shí)現(xiàn)不同縮放動(dòng)畫效果,感興趣的可以動(dòng)手嘗試一下2022-06-06android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng)
這篇文章主要為大家詳細(xì)介紹了android studio開發(fā)實(shí)現(xiàn)APP開機(jī)自啟動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05