简体   繁体   中英

How read chunk files and show as pdf in android?

Is it possible to convert a pdf file to some chunks and then read that chunks and display that chunks in PdfViewer android? If it is possible, please guide me how do that.

I found this method for convert pdf to chunks:

private void getBytesFromFile(File file) throws IOException {
    FileInputStream is = new FileInputStream(file); //videorecorder stores video to file

    java.nio.channels.FileChannel fc = is.getChannel();
    java.nio.ByteBuffer bb = java.nio.ByteBuffer.allocate(10000);

    int chunkCount = 0;

    byte[] bytes;

    while(fc.read(bb) >= 0){
        bb.flip();
        //save the part of the file into a chunk
        bytes = bb.array();
        storeByteArrayToFile(bytes, mRecordingFile + "." + chunkCount);//mRecordingFile is the (String)path to file
        chunkCount++;
        bb.clear();
    }
}

private void storeByteArrayToFile(byte[] bytesToSave, String path) throws IOException {
    FileOutputStream fOut = new FileOutputStream(path);
    try {
        fOut.write(bytesToSave);
    }
    catch (Exception ex) {
        Log.e("ERROR", ex.getMessage());
    }
    finally {
        fOut.close();
    }
}

I used AndroidPdfViewer for displaying pdf but if you know other library or method to show pdf, please mention it. Thanks.

You can achieve this with help of PDF Linearization. Basically this is a standard feature in PDF where data is organized in such way to enable on-demand streaming of content. For this to work you will also need to use a PDF Viewer that supports linearization (for example https://www.pdftron.com/documentation/android/guides/viewer/view-online-pdfs/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM