Android中從assets資源中讀取圖片文件並保存到內部存儲器並加載顯示在ImageView中


場景

Android系統為每個新設計的程序提供了/assets目錄,這個目錄保存的文件可以打包在程序里。/res和/assets的不同點是,android不為/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路徑和文件名。assets 可以被看做隨應用打包的微型文件系統,支持任意層次的文件目錄結構。正因為這點,游戲這種需要加載大量圖片和聲音資源的應用通常都會使用它。assets目錄中的所有文件都會隨應用打包。assets類資源放在工程根目錄的assets子目錄下,它里面保存的是一些原始的文件,可以以任何方式來進行組織。這些文件最終會被原裝不動地打包在apk文件中。如果我們要在程序中訪問這些文件,那么就需要指定文件名來訪問。

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

在res下右鍵New-Folder-Assets Folder

 

 

然后確認assets的位置

 

 

點擊Finish即可,然后在assets下添加一個文件

 

 

新建一個activity,並在布局xml文件中添加如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".AssetsActivity">

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取assets並保存到內部存儲器"/>

    <Button
        android:id="@+id/btn_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="加載內部存儲器照片並顯示在ImageView"/>

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="350dp"/>
</LinearLayout>

 

然后修改activity的代碼

package com.badao.androidstudy;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class AssetsActivity extends AppCompatActivity {

    private Button btn_save;
    private Button btn_show;
    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assets);
        btn_save = findViewById(R.id.btn_save);
        btn_show = findViewById(R.id.btn_show);
        imageView = findViewById(R.id.imageview);

        //實現將圖片文件從assets資源文件夾中讀取並保存到本機內部存儲器中
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AssetManager manager = getAssets();
                InputStream is = null;
                FileOutputStream fos = null;
                try {
                    //采用邊讀邊寫的方式
                    is = manager.open("badao.png");
                    fos = openFileOutput("badao.png",Context.MODE_PRIVATE);
                    byte[] buffer = new byte[1024];
                    int len = -1;
                    while ((len = is.read(buffer)) !=-1)
                    {
                        fos.write(buffer,0,len);
                    }
                    Toast.makeText(AssetsActivity.this,"保存成功",Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if(fos!=null)
                    {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(fos!=null)
                    {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        //將圖片從內部存儲器中加載出來並顯示在ImageView中
        btn_show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String filePath = getFilesDir().getAbsolutePath()+"/badao.png";
                Bitmap bitmap = BitmapFactory.decodeFile(filePath);
                imageView.setImageBitmap(bitmap);
            }
        });
    }
}

 

點擊第一個按鈕將assets下的圖片保存到內部存儲器中

 

 

然后會在data/data/包名/files下查看到保存的文件

 

 

然后點擊第二個按鈕從內部存儲器中加載圖片並顯示在ImageView中

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM