1. 程式人生 > >Android assets檔案使用

Android assets檔案使用

1、在main資料夾下建立assets資料夾

2、assets資料夾下可建立image資料夾,text資料夾,image資料夾下存放jpg或png圖片。text資料夾下存放.txt文字

3、讀取assets檔案內容:

entity類:

public class Book {
    private static final String TAG = "Book";
    private String mBookTitle;
    private Bitmap mBookCover;
    //格式化文字,將文字以段落為單位儲存
    private List<String> mParagraphList;

    //目錄集合(卷/章/回/集等)
    private List<String> mBookContents;
    //目錄對應的在段落集合中的索引
    private List<Integer> mContentParaIndexs;

    //空兩格
    private String mSpace = "\t\t\t\t\t\t";

    public Book(String bookTitle, Bitmap bookCover, String fullText) {
        mParagraphList = new ArrayList<>();
        mBookContents = new ArrayList<>();
        mContentParaIndexs=new ArrayList<>();

        mBookTitle = bookTitle;
        mBookCover = bookCover;

        formatText(fullText);

        findContents(mParagraphList);
    }

    //格式化文字,將文字以段落為單位儲存
    private void formatText(String text) {
        boolean isFirstParas = true;
        String paragraph = "";

        //按段落切分文字
        String[] paragraphs = text.split("\\s{2,}");
        //格式化段落
        for (int i = 0; i < paragraphs.length; i++) {
            if (paragraphs[i].isEmpty()) {
                continue;
            }
            if (isFirstParas) {
                paragraph = mSpace + paragraphs[i];
                isFirstParas = false;
            } else {
                paragraph = "\n" + mSpace + paragraphs[i];

            }

            mParagraphList.add(paragraph);

        }

    }



    private void findContents(List<String> paraList) {
        //字串匹配模式
        String patternString = "第\\S{2,4}\\s\\S{2,}";
        Pattern pattern = Pattern.compile(patternString);

        for (String para:paraList) {

            Matcher matcher = pattern.matcher(para);

           if (matcher.find()){

               //除去段首多餘空格
                int start = matcher.start();
                int end = matcher.end();
                String subString = para.substring(start, end);

                mBookContents.add(subString);   //目錄
                mContentParaIndexs.add(paraList.indexOf(para)); //目錄對應的在段落集合中的索引

            }

        }

    }


    public String getBookTitle() {
        return mBookTitle;
    }

    public Bitmap getBookCover() {
        return mBookCover;
    }

    public List<String> getParagraphList() {
        return mParagraphList;
    }

    public List<String> getBookContents() {
        return mBookContents;
    }

    public List<Integer> getContentParaIndexs() {
        return mContentParaIndexs;
    }
}

 

載入類:

public class BookLab {
    public static final String TEXT = "text";
    public static final String IMAGE = "image";
    private static BookLab sBookLab;

    private AssetManager mAssetManager;
    private List<Book> mBookList;
    //assets中的檔名清單
    private String[] mAssetsImageList;
    private String[] mAssetsTextList;


    private BookLab(Context context) {
        mAssetManager = context.getAssets();
        loadAssetsFiles();
    }


    public static BookLab newInstance(Context context) {
        if (sBookLab == null) {
            sBookLab = new BookLab(context);
        }
        return sBookLab;
    }

    //載入assets中的檔案
    private void loadAssetsFiles() {
        mBookList = new ArrayList<>();
        //獲取image、text中的檔名清單
        try {
            mAssetsImageList = mAssetManager.list(IMAGE);
            mAssetsTextList = mAssetManager.list(TEXT);
        } catch (IOException e) {
            e.printStackTrace();
        }


        for (int i = 0; i < mAssetsTextList.length; i++) {
            //獲取書名
            String[] nameSplit = mAssetsTextList[i].split("_");
            String nameSecond = nameSplit[nameSplit.length - 1];
            String bookTitle = nameSecond.replace(".txt", "");

            //獲取封面
            String imagePath = IMAGE + "/" + mAssetsImageList[i];
            Bitmap bookCover = loadImage(imagePath);

            //獲取文字
            String textPath = TEXT + "/" + mAssetsTextList[i];
            String bodyText = loadText(textPath);


            Book book = new Book(bookTitle, bookCover, bodyText);
            mBookList.add(book);

        }

    }


    //從assets中讀取文字
    private String loadText(String path) {
        InputStream in = null;
        BufferedReader reader = null;
        StringBuilder stringBuilder = new StringBuilder();

        try {
            in = mAssetManager.open(path);
            reader = new BufferedReader(new InputStreamReader(in));

            String line = "";
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return stringBuilder.toString();

    }

    //從assets中讀取圖片
    private Bitmap loadImage(String path) {
        Bitmap image = null;
        InputStream in = null;
        try {
            in = mAssetManager.open(path);
            image = BitmapFactory.decodeStream(in);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return image;
    }

    public List<Book> getBookList() {
        return mBookList;
    }
}

activity讀取資料:

private List<Book> mBookList;
mBookList = BookLab.newInstance(this).getBookList();