0
        byte[] pdfBinary ;
        File file1 = new File("/mnt/sdcard/","metaphysics.pdf");
        Log.d(TAG, "got a file reference");
        Uri uri1 = Uri.fromFile(file1);

        pdffilename = file1.getName();
        Log.d(TAG, "got File name");
        pdfBinary = readUriContent(uri1);
        Log.d(TAG, "read from uri");

i was trying to get the thumbnail of a PDF file. To do this I have to store My PDF file in the RAM in form of binary. for small files it is good but for large files like 20MB it is showing java.lang.OutofMemoryError. any suggestions how can i do it.

thanks in advance.

1 Answers1

0

Take a look at this answer.

The idea: map a buffer to the file channel and read just the first page. That should reduce memory consumption.

Pdf renderer has also been ported to android: Andpdf

I hope this will help you.

Community
  • 1
  • 1
Stephan
  • 4,395
  • 3
  • 26
  • 49
  • thanks for reply. I know about **Andpdf** and I am using their code. but the problem is with out of memory exception. small files like 500KBs are doing good – crack_addict Feb 16 '12 at 10:09
  • ' private byte[] readUriContent(Uri uri) { byte[] result = null; try { InputStream is = getContentResolver().openInputStream(uri); int size = is.available(); result = new byte[size]; int pos = 0; int cnt = is.read(result, pos, size-pos); while (cnt > 0) { pos += cnt; cnt = is.read(result, pos, size-pos); } is.close(); } catch (Exception e) { Log.e(TAG, e.getMessage(), e); } return result; } ' – crack_addict Feb 16 '12 at 10:26
  • Please test reading the file with Java NIO as posted in my link above. That should consume less memory than reading the hole file into a byte buffer because it only maps the file system buffer to Java. – Stephan Feb 16 '12 at 10:56