Android存儲卡讀寫文件與Application數據保存的實現介紹
一、在存儲卡上讀寫圖片文件
Android的位圖工具是Bitmap,App讀寫B(tài)itmap可以使用性能更好的BufferedOutputStream和BufferedInputStream。
Android還提供了BitmapFactory工具用于讀取各種來源的圖片,相關方法如下:
- decodeResource:該方法可從資源文件中讀取圖片信息。
- decodeFile:該方法可將指定路徑的圖片讀取到Bitmap對象。
- decodeStream:該方法從輸入流中讀取位圖數據。
例:點擊保存再點擊讀取后出現圖片
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"; // 獲取當前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 { // 把位圖數據保存到指定路徑的圖片文件 public static void saveImage(String path, Bitmap bitmap) { FileOutputStream fos = null; try { fos = new FileOutputStream(path); // 把文件數據壓縮到文件輸出流 bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } //從指定路徑的圖片文件中讀取位圖數據 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標簽,沒有指定則使用默認;
可以新建一個MyApplication類繼承Application,然后在清單文件中加入以下代碼指定使用自定義的application。
android:name=".MyApplication"
MyApplication類
public class MyApplication extends Application { //App啟動時調用 public void onCreate() { super.onCreate(); mApp = this; } //App終止調用 @Override public void onTerminate() { super.onTerminate(); } //App配置改變調用,如豎屏變?yōu)闄M屏 @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); } }
2、利用Application操作全局變量
全局的意思是其他代碼都可以引用該變量,因此全局變量是共享數據和消息傳遞的好方法。
適合在Application中保存的全局變量主要有:
- 會頻繁讀取的信息,如用戶名、手機號等。
- 不方便由意圖傳遞的數據,如位圖對象、非字符串類型的集合對象等。
- 容易因頻繁分配內容而導致內存泄漏的對象,如Handler對象等。
例:輸入信息點擊保存,只要應用不掛掉,重新回到應用還會有輸入的信息。
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; // 聲明一個公共的信息映射對象,可當作全局變量使用 public HashMap<String,String> infoMap = new HashMap<>(); public static MyApplication getInstance(){ return mApp; } //App啟動時調用 public void onCreate() { super.onCreate(); mApp = this; } //App終止調用 @Override public void onTerminate() { super.onTerminate(); } //App配置改變調用,如豎屏變?yōu)闄M屏 @Override public void onConfigurationChanged(@NonNull Configuration newConfig) { super.onConfigurationChanged(newConfig); } }
到此這篇關于Android存儲卡讀寫文件與Application數據保存的實現介紹的文章就介紹到這了,更多相關Android存儲卡讀寫文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android應用中ListView利用OnScrollListener分頁加載數據
這篇文章主要介紹了Android應用中ListView利用OnScrollListener分頁加載數據的方法,包括對OnScrollListener事件順序次數的分析,需要的朋友可以參考下2016-03-03Android開發(fā)實現可拖動排序的ListView功能【附源碼下載】
這篇文章主要介紹了Android開發(fā)實現可拖動排序的ListView功能,結合實例形式分析了Android列表拖動排序原理與相關操作技巧,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11android studio開發(fā)實現APP開機自啟動
這篇文章主要為大家詳細介紹了android studio開發(fā)實現APP開機自啟動,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05