亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android存儲(chǔ)卡讀寫文件與Application數(shù)據(jù)保存的實(shí)現(xiàn)介紹

 更新時(shí)間:2022年09月27日 10:55:17   作者:Shewyoo  
這篇文章主要介紹了Android在存儲(chǔ)卡上讀寫文件、Application保存數(shù)據(jù)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

一、在存儲(chǔ)卡上讀寫圖片文件

Android的位圖工具是Bitmap,App讀寫B(tài)itmap可以使用性能更好的BufferedOutputStream和BufferedInputStream。

Android還提供了BitmapFactory工具用于讀取各種來源的圖片,相關(guān)方法如下:

  1. decodeResource:該方法可從資源文件中讀取圖片信息。
  2. decodeFile:該方法可將指定路徑的圖片讀取到Bitmap對(duì)象。
  3. 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)文章

最新評(píng)論