0

i'm trying to pass on sdcard a pdf from my raw folder to open it with a default intent.

When I open it, it's said to me that the pdf is dammaged. since I don't quite understants how stream works, I think that the issue comes frome there.

final String extStorageDirectory = Environment
                        .getExternalStorageDirectory().toString();
                final String festivalDirectory_path = extStorageDirectory
                        + Constants.PDF_STORAGE_PATH;
                File pdfOutputFile = new File(festivalDirectory_path, "/");
                if (pdfOutputFile.exists() == false) {
                    pdfOutputFile.mkdirs();
                }
                File pdfFile = new File(pdfOutputFile, nameOfThePdf);

                try {
                    InputStream in = getResources().openRawResource(R.raw.blablublo);

                    FileOutputStream out = new FileOutputStream(pdfFile);
                    byte[] buffer = new byte[1024];
                    int bufferLength = 0;
                    while ((bufferLength = in.read(buffer)) > 0) {
                        out.write(buffer, 0, bufferLength);
                    }
                    out.close();
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
                Uri path = Uri.fromFile(pdfFile);
//              Uri path = Uri.parse(Constants.SPLASH_URI + R.raw.cartecannotpdf);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

Hope that someone could help me !

thx

Renaud

Renaud Favier
  • 391
  • 6
  • 20

1 Answers1

0

Well, it looks like your solution is half finished.

InputStream inputStream = assetManager.open("cartecanotpdf.pdf"); - Creates a stream which you can use to read all the data from the PDF file in the assets folder.

File file = new File(dir, "cartecannotpdf.pdf"); - This just creates a link to a location on the SDCARD where the file will be located.

Teh part you're missing is actually copying bytes from the inputstream to the outputstream. At the moment, the file on the SDCARD is either non-existant, or it's just an empty shell (doesn't have any data), so your intent will complain that it's corrupted.

Here is a QA that has a full solution for you. How to copy files from 'assets' folder to sdcard?

Edit: In addition, I'm not sure if you need to put the file in your assets folder, or if you can leave it in your RAW folder. I've always worked with files in my assets folder. Make sure you are actually getting a file when you call assetManager.open(...)

Community
  • 1
  • 1
Gophermofur
  • 2,101
  • 1
  • 14
  • 14
  • ok sorry for the delay, I was tring to modify my code as you both said, It still doesn't work. Thought I think that I'm now well filling the sdcard file. I don't see other way to read a pdf than this one – Renaud Favier May 07 '12 at 14:31
  • 1) Try calling `out.flush();` before `out.close()` 2) What are the file sizes of hte original and copied PDF? – Gophermofur May 07 '12 at 14:40
  • Try changing `FileOutputStream out = new FileOutputStream(pdfFile);` to `OutputStream out = new FileOutputStream(pdfFile);` – Gophermofur May 07 '12 at 14:46
  • First: Set a breakpoint at the start of that method and step through each line of code. I have a feeling that you are hitting exceptions, but because they get logged, you aren't noticing. Move your PDF file to your assets folder instead of your RAW folder. If you want to keep it in the RAW folder, you need to access it in other ways see the following: http://stackoverflow.com/questions/5771366/reading-a-simple-text-file – Gophermofur May 07 '12 at 16:07
  • Ok it was indeed the way i acceded to my resources, with getRaw, it worked ! thx very much – Renaud Favier May 08 '12 at 12:44