Reputation: 1
I placed a pdf file (bang.pdf) in assets folder, i need to open and display the pdf from assets folder when a button is clicked. The code is :
package com.example.pdfviewer;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button but1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button)findViewById(R.id.but1);
but1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"hai" , Toast.LENGTH_SHORT).show();
CopyReadAssets();
}
});
}
private void CopyReadAssets()
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "bang.pdf");
try
{
in = assetManager.open("bang.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() + "/bang.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);
}
}
}
But when i click the button , the following error is displayed Error : - android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///data/data/com.example.pdfviewer/files/bang.pdf typ=application/pdf } in mainifestfile:- i mentioned I have installed adobe reader application in my device, still getting the same error message.
please help me on this
Thanks in advance
Upvotes: 0
Views: 8730
Reputation: 1006
I see the answer is given here:
Read a pdf file from assets folder
(it is almost as the question yet additional item is added, the missing request for permission WRITE_EXTERNAL_STORAGE). file:// seems to be okay and is referring to the external storage.
Maybe there are better ways than to copy outside (copy to internal storage + ContentProvider as suggested by Siddharth Vyas ?).
Upvotes: 0
Reputation: 10100
file://android_asset only works in your app
You will need to either:
Hope this helps.
Upvotes: 1