Android assets文件使用

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中读取图片
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值