How to Open a PDF Stored Either in Res/Raw or Assets Folder

How to open a pdf stored either in res/raw or assets folder?

You would be able to show it from raw/ or assets/ if your application actually implemented a PDF reader. Since you want it to be displayed in a separate application (such as Adobe Reader), I suggest doing the following:

  1. Store the PDF file in the assets/ directory.
  2. When the user wants to view it, copy it somewhere public. Look into openFileOutput or getExternalFilesDir.
  3. Launch the Intent just like you are doing now, except use getAbsolutePath() on the newly created file for the intent's data.

Be aware that a user might not have a PDF reading application. In this case, it is useful to catch the ActivityNotFoundException and show an appropriate message.

How to view pdf from assets or raw folder?

Try below code

 private void readAssetAndMakeCopy()
{
AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "git.pdf");
try
{
in = assetManager.open("git.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/git.pdf"),
"application/pdf");

startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}

Make sure to include

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in manifest

Access PDF files from 'res/raw' or assets folder programmatically to parse with given methods

After many many hours and quite a few cigarette breaks here is the solution. Once the readToByteBuffer has returned a ByteBuffer it is as easy as creating a new PDFFile that takes in a ByteBuffer.

Enjoy...

ShowPDF button Listener...

OnClickListener ShowPdfListener = new OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(PdfFileSelectActivity.this,
PdfViewerActivity.class);
startActivity(intent);
}
};

In onCreate() PdfViewerActivity...

openFile2(readToByteBuffer(this.getAssets().open("test.pdf")), null);

edited readToByteBuffer() from here

public ByteBuffer readToByteBuffer(InputStream inStream) throws IOException
{
long startTime = System.currentTimeMillis();
BufferedReader in = new BufferedReader(new InputStreamReader(this.getAssets().open("test.pdf")));
StringBuilder total = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
total.append(line);
}

int length = total.length();
byte[] buffer = new byte[length];
ByteArrayOutputStream outStream = new ByteArrayOutputStream(length);
int read;
while (true) {
read = inStream.read(buffer);
if (read == -1)
break;
outStream.write(buffer, 0, read);
}
ByteBuffer byteData = ByteBuffer.wrap(outStream.toByteArray());
long stopTime = System.currentTimeMillis();
mGraphView.fileMillis = stopTime-startTime;
return byteData;
}

Reading a pdf file placed in assets folder in android

Is this file in your app's assets folder? If it is, then your URI is wrong, check out this question:

How to open a pdf stored either in res/raw or assets folder?

there's an answer describing how to construct a proper URIs for your assets.

Android - Access file from assets \ PDF display

As Barak said you can copy it out of assets to internal storage or the SD card and open it from there using inbuilt pdf applications.

Following Snippet will help you.
(I have updated this code to write to and read files from internal storage.

But i dont recommend this approach because pdf file can be more than 100mb in size.

So its not recommended to save that huge file into internal storage

Also make sure while saving file to internal storage you use

openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

Then only other applications can read it.

Check following snippet.

package org.sample;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class SampleActivity extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
CopyReadAssets();

}

private void CopyReadAssets()
{
AssetManager assetManager = getAssets();

InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "git.pdf");
try
{
in = assetManager.open("git.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Log.e("tag", e.getMessage());
}

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/git.pdf"),
"application/pdf");

startActivity(intent);
}

private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}

}

Make sure to include

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in manifest



Related Topics



Leave a reply



Submit