view pdf in android using program or javascript - android

I'm new to android development. I have a link in HTML page which is having a PDF document URL as h ref, while clicking the link i need to open the PDF in adobe reader. if any one knows how to do guide me.

Try this :
public class OpenPdf extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
Hope this helps.

Related

In correct url to display a pdf on android

I'm trying to display a local pdf file on my android app but it says that the pdf file doesn't exists ,the pdf file it's in the project's assets folder
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_programa);
verPdf = (Button)findViewById(R.id.button1);
verPdf.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Uri file= Uri.parse("file:///assets/miarchivo.pdf");
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(file.toString()));
try{
Intent i;
i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(file,mimeType);
startActivity(i);
}catch (ActivityNotFoundException e) {
Toast.makeText(v.getContext(),
"No Application Available to fiew this file type",
Toast.LENGTH_SHORT).show();
}
}
});
}

How can I open a PDF file via my Android app?

I want to open a pdf file via an Android app. So far I have written the following code:
public class PdfopenActivity extends Activity {
String selectedFilePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// Sample.pdf is my file name it is in /Root/Download/Sample.pdf
// path of the file
File file = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sample.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file),"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
}
The pdf is 175kb and I can open the file directly on my Galaxy Tab2 tablet, but when I run my my program to open it I get the error:
An Error Occurred while opening the document.
Can anyone tell me where I am going wrong?
.
Try this out:
Button btnOpen=(Button)findViewById(R.id.button1);
btnOpen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/sample.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
}
}
}
});
Hope this will help you.

Opening pdf in android/kindle app

I want to open a pdf from a website onto an app i'm making for the kindle and android.
I think my code is right but it can't find my file and I wasn't sure if anyone knew if I should be formatting the file different since it's technically a website....
Any input would be greatly appreciated. Here's what I have so far:
Button OpenPDF = (Button) findViewById(R.id.button1);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("http://thisisthewebsitewithpdf.pdf");
if(pdfFile.exists()) //EXCEPTION HERE. pdfFile doesn't exist!
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(pdfIntent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(PDFActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
you can download the pdf to your sdcard and then open it using this activitty:
public class OpenPdf extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.OpenPdfButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File("/sdcard/example.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
}

How PDf file can be read through android via coding

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button OpenPDF = (Button) findViewById(R.id.button);
OpenPDF.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
File pdfFile = new File("res/raw/comic.pdf");
if(pdfFile.exists())
{
//Uri path = Uri.fromFile(pdfFile);
Uri path = Uri.parse("android.resource://" + getPackageName() + "/ R.raw.comic.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
// Intent pdfIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://package.name/" + pdfFile));
// pdfIntent.setDataAndType(path, "/assets/arduino-comic-latest.pdf");
// pdfIntent.setType("application/pdf");
// pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
I have done this code but this is not find the path of the file.what I have to do??can Anyone help me
Thankyou
you cant do that directly, you have to use Native Library for reading PDF... library may be iText, jPedal, Mupdf, Pdfbox... etc. Google it

android : how to make my apps open specific PDF file on click

I am wondering if my codes here can view a specific pdf file using existing PDF viewer in Android Phone (HTC Desire).. If i would like to open pdf files from local folder.. What should i do?
public class ghcm_Submenu1 extends Activity {
private ListView lv1;
private String lv_arr[]= {"item1", "item2"};
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.submenu);
lv1=(ListView)findViewById(R.id.ListView01);
// By using setAdpater method in listview we an add string array in list.
lv1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , lv_arr));
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parentView, View childView, int position, long id) {
if ((position)== 0){
Intent intent = new Intent();
File file = new File("/sdcard/item1.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(ghcm_Submenu1.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
else if ((position)== 1){
Intent intent = new Intent();
File file = new File("/sdcard/item2.pdf");
if (file.exists()) {
Uri path = Uri.fromFile(file);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(ghcm_Submenu1.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
}
}
public void onNothingSelected(AdapterView parentView) {
}
});
}
}

Categories

Resources