Android开发(2)--Android资源访问机制

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lwplwf/article/details/83149349

在开发中需要引用程序资源,比如项目中assets和res目录下的图片、layout、values等或者需要系统内置的资源。

资源分为两种:
第一种:res目录下的资源(不会被编译,但是会生成id)
第二种:assets文件夹下的资源文件,又叫原始资源文件(不会被编译,也不会生成id)

1、创建Assets文件夹

右键目标文件夹进行创建
在这里插入图片描述

2、获取Assets文件夹的管理类

AssetManager assets = getAssets();

3、遍历文件夹下的资源列表

String[] list = assets.list();

app/src/main/java/lwplw.com.helloworld/FileUtils.java文件中有:

String fileNames[] = context.getAssets().list(srcPath);

这句就相当于一次实现了上面的2和3。

4、获取Assets文件夹的资源

5、示例程序

package lwplw.com.helloworld;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class FileUtils {

    private static FileUtils instance;
    private static final int SUCCESS = 1;
    private static final int FAILED = 0;
    private Context context;
    private FileOperateCallback callback;
    private volatile boolean isSuccess;
    private String errorStr;

    public static FileUtils getInstance(Context context) {
        if (instance == null)
            instance = new FileUtils(context);
        return instance;
    }

    private FileUtils(Context context) {
        this.context = context;
    }

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (callback != null) {
                if (msg.what == SUCCESS) {
                    callback.onSuccess();
                }
                if (msg.what == FAILED) {
                    callback.onFailed(msg.obj.toString());
                }
            }
        }
    };

    public FileUtils copyAssetsToSD(final String srcPath, final String sdPath) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                copyAssetsToDst(context, srcPath, sdPath);
                if (isSuccess)
                    handler.obtainMessage(SUCCESS).sendToTarget();
                else
                    handler.obtainMessage(FAILED, errorStr).sendToTarget();
            }
        }).start();
        return this;
    }

    public void setFileOperateCallback(FileOperateCallback callback) {
        this.callback = callback;
    }

    private void copyAssetsToDst(Context context, String srcPath, String dstPath) {
        try {
            String fileNames[] = context.getAssets().list(srcPath);
            if (fileNames.length > 0) {
                File file = new File(Environment.getExternalStorageDirectory(), dstPath);
                if (!file.exists()) file.mkdirs();
                for (String fileName : fileNames) {
                    if (!srcPath.equals("")) { // assets 文件夹下的目录
                        copyAssetsToDst(context, srcPath + File.separator + fileName, dstPath + File.separator + fileName);
                    } else { // assets 文件夹
                        copyAssetsToDst(context, fileName, dstPath + File.separator + fileName);
                    }
                }
            } else {
                File outFile = new File(Environment.getExternalStorageDirectory(), dstPath);
                InputStream is = context.getAssets().open(srcPath);
                FileOutputStream fos = new FileOutputStream(outFile);
                byte[] buffer = new byte[1024];
                int byteCount;
                while ((byteCount = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, byteCount);
                }
                fos.flush();
                is.close();
                fos.close();
            }
            isSuccess = true;
        } catch (Exception e) {
            e.printStackTrace();
            errorStr = e.getMessage();
            isSuccess = false;
        }
    }

    public interface FileOperateCallback {
        void onSuccess();
        void onFailed(String error);
    }

}

6、使用举例

app/src/main/java/lwplw.com.helloworld/MainActivity.java文件
函数功能:
(1)在手机存储卡创建目录结构lwplw_helloworld/data
(2)把assets文件夹中的资源,全部copy倒sd卡lwplw_helloworld/data目录

在这里插入图片描述

7、注意事项

(1)assets文件是压缩在apk中的,apk运行时无法直接访问该目录,需要copy出来。
(2)需要等到assets文件中资源全部copy完成后,回调到 onSuccess(),再执行后续操作。

猜你喜欢

转载自blog.csdn.net/lwplwf/article/details/83149349