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中

博客园: https://www.cnblogs.com/badaoliumangqizhi/ 关注公众号 霸道的程序猿 获取编程相关电子书、教程推送与免费下载。
原文地址:https://www.cnblogs.com/badaoliumangqizhi/p/14076807.html