FolioReader / FolioReader-Android

A Java ePub reader and parser framework for Android.
BSD 3-Clause "New" or "Revised" License
2.25k stars 718 forks source link

Is it possible reading epub file directly from assetes location? #262

Open JFox-sk opened 6 years ago

JFox-sk commented 6 years ago

Why folioreader not reading epub file from assets location, but first copy this file to sd card and then read it from sd card? Is it possible reading epub file directly from assetes location?

hrishikesh-kadam commented 6 years ago

@JFox-sk Assets and Raw resources are bundled with APK. They cannot be directly read as File. They have to be lazy loaded as a File first.

katcom commented 6 years ago

Actually, I saw enhancement. The cache file, I think, should be generated in app directory rather than in sdcard. To save caches in sdcard is not safe, since they can be easily deleted by users or other applications.

The code from Internet illustrates how a cache of epub can be generate within app directory.

File file = new File(mContext.getCacheDir(), filePath);

            if (!file.exists()) {
                // Since FolioReader cannot handle the compressed asset file directly, we copy it into
                // the cache directory.
                InputStream asset = context.getAssets().open(filePath);
                file.createNewFile();
                FileOutputStream output = new FileOutputStream(file);
                final byte[] buffer = new byte[1024];
                int size;
                while ((size = asset.read(buffer)) != -1) {
                    output.write(buffer, 0, size);
                }
                asset.close();
                output.close();
            }