Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sometimes Can't read assets folder's pdf file #6

Closed
TheMatrix19 opened this issue May 18, 2016 · 5 comments
Closed

Sometimes Can't read assets folder's pdf file #6

TheMatrix19 opened this issue May 18, 2016 · 5 comments

Comments

@TheMatrix19
Copy link

TheMatrix19 commented May 18, 2016

i m using the same code for assets folder that are given in the pdfViewPager example.
This code sometimes read assets folder's pdf file but sometime it can not read the file and screen is shown blank and it gives error file is not available.
file from sd card always read and successfully show in app and when the sd card file shows than i change the code to show file through assets folder without taking sd card read permission and the file successfully shows..
will you please tell me the right way to read assets folder file.
i am using this code for copy assets sometime it successfully read the file but sometimes fail.

final String[] sampleAssets = {"adobe.pdf","sample.pdf"};
@OverRide
public void onCreate() {
super.onCreate();
initSampleAssets();
}
private void initSampleAssets() {
CopyAsset copyAsset = new CopyAssetThreadImpl(this, new Handler());
for(String asset : sampleAssets){
copyAsset.copy(asset, new File(getCacheDir(), asset).getAbsolutePath());
}
}

@voghDev
Copy link
Owner

voghDev commented May 26, 2016

Your code is fine.
The exact code that performs the copy is this one:

public static boolean copyAsset(Context ctx, String assetName, String destinationPath) throws IOException{
    InputStream in = ctx.getAssets().open(assetName);
    File f = new File(destinationPath);
    f.createNewFile();
    OutputStream out = new FileOutputStream(new File(destinationPath));

    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
        out.write(buffer, 0, read);
    }
    in.close();
    out.close();

    return true;
}

You could call this function and copy the assets on SD card by yourself, without using a CopyAsset object. You can then log what getAssets() and open() are returning, and find the error.

It never happened to me. If you find a trace where the code is failing, post it so I can review what's going on.

@voghDev
Copy link
Owner

voghDev commented Jun 9, 2016

Hi again,

have you finally managed to read the assets folder? was just curious about the root cause of this issue.

@narendra9593
Copy link

narendra9593 commented Jun 9, 2016

Code
public class PDFViewPagerActiivty extends Activity {

PDFViewPager pdfViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    pdfViewPager = (PDFViewPager) findViewById(R.id.pdfViewPagerZoom);
}
@Override
protected void onDestroy() {
    super.onDestroy();

    BasePDFPagerAdapter adapter = (BasePDFPagerAdapter) pdfViewPager.getAdapter();

    if(adapter != null) {
        adapter.close();
        adapter = null;
    }
}

}

Layout --------------------
<es.voghdev.pdfviewpager.library.PDFViewPager
android:id="@+id/pdfViewPagerZoom"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:assetFileName="adobe.pdf"
/>

ERROR

java.io.FileNotFoundException: No such file or directory
W/System.err: at android.os.Parcel.openFileDescriptor(Native Method)
W/System.err: at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:270)
W/System.err: at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:200)
W/System.err: at es.voghdev.pdfviewpager.library.adapter.BasePDFPagerAdapter.getSeekableFileDescriptor(BasePDFPagerAdapter.java:98)
W/System.err: at es.voghdev.pdfviewpager.library.adapter.BasePDFPagerAdapter.init(BasePDFPagerAdapter.java:62)
W/System.err: at es.voghdev.pdfviewpager.library.adapter.BasePDFPagerAdapter.(BasePDFPagerAdapter.java:56)
W/System.err: at es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter.(PDFPagerAdapter.java:37)
W/System.err: at es.voghdev.pdfviewpager.library.PDFViewPager.initAdapter(PDFViewPager.java:62)
W/System.err: at es.voghdev.pdfviewpager.library.PDFViewPager.init(PDFViewPager.java:57)
W/System.err: at es.voghdev.pdfviewpager.library.PDFViewPager.(PDFViewPager.java:37)
W/System.err: at java.lang.reflect.Constructor.newInstance(Native Method)
W/System.err: at android.view.LayoutInflater.createView(LayoutInflater.java:619)
W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
W/System.err: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
W/System.err: at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
W/System.err: at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
W/System.err: at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
W/System.err: at android.transition.Scene.enter(Scene.java:175)
W/System.err: at com.android.internal.policy.PhoneWindow.transitionTo(PhoneWindow.java:451)
W/System.err: at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:391)
W/System.err: at android.app.Activity.setContentView(Activity.java:2166)
W/System.err: at com.app.android.cync.SplashActiivty.onCreate(SplashActiivty.java:24)
W/System.err: at android.app.Activity.performCreate(Activity.java:6237)
W/System.err: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
W/System.err: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
W/System.err: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
W/System.err: at android.app.ActivityThread.-wrap11(ActivityThread.java)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err: at android.os.Looper.loop(Looper.java:148)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5417)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                  [ 06-09 06:57:04.955 28201:28201 D/         ]
                                                                  HostConnection::get() New Host Connection established 0xab0f2da0, tid 28201

loaded /system/lib/egl/libEGL_emulation.so
/system/lib/egl/libGLESv1_CM_emulation.so
loaded /system/lib/egl/libGLESv2_emulation.so

                                                          [ 06-09 06:57:05.079 28201:28240 D/         ]
                                                          HostConnection::get() New Host Connection established 0xab0f2e80, tid 28240

I/OpenGLRenderer: Initialized EGL, version 1.4
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab0ebcc0, error=EGL_SUCCESS
E/System: Uncaught exception thrown by finalizer
E/System: java.lang.NullPointerException: Attempt to invoke virtual method 'void dalvik.system.CloseGuard.warnIfOpen()' on a null object reference
at android.graphics.pdf.PdfRenderer.finalize(PdfRenderer.java:210)
at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:202)
at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:185)
at java.lang.Thread.run(Thread.java:818)

@voghDev
Copy link
Owner

voghDev commented Aug 9, 2016

@narendra9593: Maybe you are trying to open the file before the copy has completed? You can, for example, set the adapter to the PdfViewPager once the copy has been completed, or open the activity once the copy has been done.

@voghDev
Copy link
Owner

voghDev commented Mar 1, 2017

Example code of my last comment:

CopyAsset copyAsset = new CopyAssetThreadImpl(getApplicationContext(), new Handler(), new CopyAsset.Listener() {
        @Override
        public void success(String assetName, String destinationPath) {
            pdfViewPager = new PDFViewPager(context, getPdfPathOnSDCard(Environment.getExternalStorageDirectory()));
            setContentView(pdfViewPager);
        }

        @Override
        public void failure(Exception e) {
            e.printStackTrace();
        }
    });

Full example available here

Let me know if it helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants