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

Am I restricted to open epub files from raw and asset folder alone? #264

Closed flxkalu closed 6 years ago

flxkalu commented 6 years ago

I am developing an ebook reader app and everything works fine when i store the epub files on the assets or raw files. But it is not possible to download and files on the assets and raw folders so i am saving files in the internal storage so that the downloaded ebooks would not be accessed by any user or other apps but when i try to open the books with the internal storage file path folio reader crashes.

Is this built to open only files in the assets and raw folders, if that is the case, please how can i make it open epub files that are stored on the mobile phone internal storage?

Please help me i have been stuck here for days and can't find any solutions yet. Thanks

hrishikesh-kadam commented 6 years ago

@flxkalu Look like #265 is duplicate of this. So let us have chat on this issue. Also can you please yourself close the duplicate issue?

Now talking about the issue, FolioReader does open files stored in app's internal storage. May I know what path are you specifying in FolioReader.openBook()?

Also now we have added Issue Template for reporting any issues - https://github.com/FolioReader/FolioReader-Android#reporting-issue

So please fill the template and reply back on same thread.

flxkalu commented 6 years ago

Thanks for the reply. I have closed the duplicate case as you mentioned.

I am downloading the epub file and storing on the internal storage using the getFilesDir().getAbsolutPath()+filename

When i try to open the epub file with the openbook method stored in that location it returns null object error.

To be more direct with answering your question,

I have a string variable called fileLocation that stores the path which is getFilesDir().getAbsolutpath()+filename, then i call the folioreader.openbook(fileLocation);

flxkalu commented 6 years ago

Issue / Feature - Issue FolioReader version - 0.3.9 FolioReader Stock / Modified - Stock Android SDK - 4.4.4 Mobile / Tablet / Emulator Info -pixel 2 nougat Crash / Error - Crash

Steps to reproduce / Describe in detail -

I'm using library version 0.3.9 When trying to open epub from my Activity, eg -

FolioReader folioReader = FolioReader; folioReader.openBook(getFilesDir.getAbsolutPath()+fileName); Getting this error - (While i'm opening the file stored on the internal storage of the device from a fragment)

08-28 21:28:24.510 16428-16428/com.folioreader.android.sample E/AndroidRuntime: FATAL EXCEPTION: main Process: com.folioreader.android.sample, PID: 16428 java.lang.NullPointerException: Attempt to invoke virtual method 'com.folioreader.FolioReader com.folioreader.FolioReader.openBook(java.lang.String)' on a null object reference at com.folioreader.android.sample.HomeActivity$DownloadFileFromURL.onPostExecute(HomeActivity.java:172) at com.folioreader.android.sample.HomeActivity$DownloadFileFromURL.onPostExecute(HomeActivity.java:106) at android.os.AsyncTask.finish(AsyncTask.java:660) at android.os.AsyncTask.-wrap1(AsyncTask.java) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:677) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

hrishikesh-kadam commented 6 years ago

@flxkalu I think you are missing a forward slash in path "/".

This is how you should open a book stored in files directory of your app's internal (private) storage. folioReader.openBook(getFilesDir().getAbsolutePath() + "/" + bookFileName);

getFilesDir().getAbsolutePath() will generate following path for sample app - /data/user/0/com.folioreader.android.sample/files

So all your downloaded data should ideally be stored in files folder of your app's internal storage.

flxkalu commented 6 years ago

i put the slash. Here is the complete code snippet from the actual code: This code runs after the download AsyncTask.

@Override protected void onPostExecute(String file_url) { dismissDialog(progress_bar_type);

        String filePath = getFilesDir().getAbsolutePath() + "/downloadedfile.epub";              
        Log.i("PATH", filePath);
        folioReader.openBook(filePath);
    }

There's already a forward slash on the code but folioreader is not opening it. This openBook method works perfectly when i put the files in the asset and raw folders.

flxkalu commented 6 years ago

Do you think this might have anything to do with the folioreader version? Do all the versions support reading epub files from any storage location. I tried to look at the entire library and saw codes relating to RAW, Assets and external storage. They were even saved as constants but i did not see anything for internal storage. Thats why i'm confused. I'm not sure where the problem is coming from.

hrishikesh-kadam commented 6 years ago

@flxkalu I think the problem would be difference between the path of actual stored location of downloaded file and the path you are serving to openBook().

Do check the path of downloaded epub file from Device File Explorer window of Android Studio.

hrishikesh-kadam commented 6 years ago

@flxkalu Can you share the downloaded epub's path, getFilesDir().getAbsolutePath()?

flxkalu commented 6 years ago

getFilesDir().getAbsolutePath() returns: /data/user/0/com.folioreader.android.sample/files

i have used the same codes to download and open different formats of image files on image view and it works perfectly.

hrishikesh-kadam commented 6 years ago

@flxkalu Also share how are you initialising the FolioReader instance. Because in the log you submitted before when specifying Issue template, you haven't initialised FolioReader correctly.

flxkalu commented 6 years ago

this is how i initialised it:

final FolioReader folioReader = FolioReader.getInstance(getActivity().getApplicationContext());

I initialised it like this because the openBook method is called inside the onPostExecute() method of the AsyncTask class ensuring that the file is completely downloaded before it tries to open it.

and this is how i called it:

folioReader.openBook(filePath);

flxkalu commented 6 years ago

i finally figured it out. I can't believe i made such petty mistake with my initialisation. I thank you so much @hrishikesh-kadam for your help. I really appreciate.

You were correct my problem was coming from initialization:

FolioReader folioReader = FolioReader.getInstance(getApplicationContext()); folioReader.openBook(filePath); solved this problem. There is not problem with folio reader. closing this now. Thanks so much