Android存儲卡讀寫文件與Application數(shù)據(jù)保存的實現(xiàn)介紹
一、在存儲卡上讀寫圖片文件
Android的位圖工具是Bitmap,App讀寫B(tài)itmap可以使用性能更好的BufferedOutputStream和BufferedInputStream。
Android還提供了BitmapFactory工具用于讀取各種來源的圖片,相關(guān)方法如下:
- decodeResource:該方法可從資源文件中讀取圖片信息。
- decodeFile:該方法可將指定路徑的圖片讀取到Bitmap對象。
- decodeStream:該方法從輸入流中讀取位圖數(shù)據(jù)。
例:點擊保存再點擊讀取后出現(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;
// 從指定的資源文件中獲取位圖對象
Bitmap b1 = BitmapFactory.decodeResource(getResources(),R.drawable.thing1);
// 把位圖對象保存為圖片文件
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運行過程中有且僅有一個Application對象貫穿整個生命周期。
清單文件AndroidMainifest.xml文件中有個application標(biāo)簽,沒有指定則使用默認(rèn);
可以新建一個MyApplication類繼承Application,然后在清單文件中加入以下代碼指定使用自定義的application。
android:name=".MyApplication"
MyApplication類
public class MyApplication extends Application {
//App啟動時調(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中保存的全局變量主要有:
- 會頻繁讀取的信息,如用戶名、手機(jī)號等。
- 不方便由意圖傳遞的數(shù)據(jù),如位圖對象、非字符串類型的集合對象等。
- 容易因頻繁分配內(nèi)容而導(dǎo)致內(nèi)存泄漏的對象,如Handler對象等。
例:輸入信息點擊保存,只要應(yīng)用不掛掉,重新回到應(yīng)用還會有輸入的信息。


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="請輸入姓名"
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;
// 聲明一個公共的信息映射對象,可當(dāng)作全局變量使用
public HashMap<String,String> infoMap = new HashMap<>();
public static MyApplication getInstance(){
return mApp;
}
//App啟動時調(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存儲卡讀寫文件與Application數(shù)據(jù)保存的實現(xiàn)介紹的文章就介紹到這了,更多相關(guān)Android存儲卡讀寫文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android使用Sensor感應(yīng)器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能
這篇文章主要介紹了Android使用Sensor感應(yīng)器實現(xiàn)線程中刷新UI創(chuàng)建android測力計的功能,實例分析了Android使用Sensor感應(yīng)器實現(xiàn)UI刷新及創(chuàng)建測力器的技巧,需要的朋友可以參考下2015-12-12
Android框架Volley使用之Post請求實現(xiàn)方法
這篇文章主要介紹了Android框架Volley使用之Post請求實現(xiàn)方法,,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05
Android實現(xiàn)圓形ProgressBar停止轉(zhuǎn)動的方法詳解
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)圓形ProgressBar停止轉(zhuǎn)動方法的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
Android 將本地資源圖片轉(zhuǎn)換成Drawable,進(jìn)行設(shè)置大小操作
這篇文章主要介紹了Android 將本地資源圖片轉(zhuǎn)換成Drawable,進(jìn)行設(shè)置大小操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
android動態(tài)布局之動態(tài)加入TextView和ListView的方法
這篇文章主要介紹了android動態(tài)布局之動態(tài)加入TextView和ListView的方法,涉及Android動態(tài)布局的實現(xiàn)技巧,需要的朋友可以參考下2015-05-05
Android library native調(diào)試代碼遇到的問題解決
這篇文章主要介紹了Android library native 代碼不能調(diào)試解決方法匯總,android native開發(fā)會碰到native代碼無法調(diào)試問題,而app主工程中的native代碼是可以調(diào)試的2023-04-04
Android實現(xiàn)文字垂直滾動、縱向走馬燈效果的實現(xiàn)方式匯總
本文給大家分享了三種方式實現(xiàn)Android文字垂直滾動、縱向走馬燈效果,文中給大家介紹了相關(guān)屬性及注意事項,需要的朋友參考下吧2017-12-12

