1

Actually, I want to download a PDF file in our Android phone and don't want to give the permission to open the PDF file in PC when he try to open by connecting the Phone to PC. so I want to go with storing the PDF file in SQLite Data Base solution.

My question is...
Is it possible to store PDF file in SQLite database? If yes, how? Or is there any other way to implement same functionality?

Please Help me...

Thanks...

not my real name
  • 393
  • 4
  • 16
KRISHNA
  • 103
  • 3
  • 9

4 Answers4

2

You can. Simply read all the bytes of the pdf file and store in the blob field of sqlite db.

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • Thanks for the reply. I am totally new in android development so can you help me with some sample code how to convert the pdf into bytes and store in the blob.... – KRISHNA Apr 27 '11 at 05:53
2

One solution is to use blob and to store the raw data of the file as column in the db.

Other solution which i think is better to store the files on the Internal Storage and in the db just to store the file paths. Internal storage is private for your application so no one will have access to the files. But be careful in both cases the files are stored on the phone storage and people can find themselves in situation when there is no more phone storage.

Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
  • Thanks for ur reply.... I am totally new in android so can you help me with some code sample how to store PDF file using blob. One thing I found...,convert the pdf to byte array and store in Blob....But not able to convert the pdf data to byte array. Thanks... – KRISHNA Apr 27 '11 at 05:41
  • You don't have to care what is the content of the pdf just convert the file to byte array. http://stackoverflow.com/questions/858980/file-to-byte-in-java – Mojo Risin Apr 27 '11 at 06:52
  • One thing dear MOjo... if the PDf File is the mixture of Image and text in this case we simple convert it into byte but again I want to convert into the pdf file and display in android Phone how? If possible help me with some code sample. – KRISHNA May 02 '11 at 09:53
  • Basically byte[] is just serialization of the file. So if you got File -> byte[] -> File you'll get the same file no matter if it's pdf or something else. Here is example how to write byte[] to File http://www.java-examples.com/write-byte-array-file-using-fileoutputstream – Mojo Risin May 02 '11 at 10:00
  • Dear Mojo many many thanks to you for helping me...Now I am able to convert byte array to pdf but that pdf file is not opened by Android PDF view,showing bad format error.Please give me some idea. – KRISHNA May 04 '11 at 15:11
  • Check if the file is identical before you serialize it to byte[] and after you deserialize it to File. If serialization works than it seems that the problem is in the pdf file itself. Investigate more :) – Mojo Risin May 05 '11 at 06:24
0

AFAIK Sqlite support the BLOB datatype, I used BLOB for store images into MySql databases, I think you can do the same thing reading the file content and storing that into a BLOB column in SQLite

MiPnamic
  • 1,257
  • 10
  • 18
0

You can use a BLOB datatype to store a PDF file in the database but that wouldnt be my suggestion since you are using SQLlite tough its possible. Some people say it's a dirty way to store binary files in a database. Large databases will result in slower queries.

My solution would be to store a PDF file in a directory and use a string pointer to the file name. This will result in faster queries and a smaller database. Only downside is that you have to write functionality to rename duplicate entries and delete files from the file system when you delete an entry.

Please comment if (large) binary files in databases make the database slower...

Antek Drzewiecki
  • 544
  • 1
  • 7
  • 18