Open PDF in android app - android

I am working on an application where need to open the pdf file in the device,
I have actually got the code on the web that is similar to most of the examples. But, the thing is that I am not able to open the file and the control goes to the "Exception" part directly.
Here is the code below:
public class MyPDFDemo extends Activity
{
/** Called when the activity is first created. */
#Override
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("/sdcard/Determine_RGB_Codes_With_Powerpoint [PDF Library].pdf");
if(pdfFile.exists())
{
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(MyPDFDemo.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
}
});
}
When I run this code : i used to see " No Application available to view pdf ". Can anyone please me to view the pdf file.

Since your catch block has ActivityNotFoundException, it means that you don't have any activity/application that can read a 'application/pdf' filetype format. Install any pdf viewer from the Android Market (Adobe recently release theirs), or else use the above mentioned open source pdf viewer and your problem will most probably will be solved.
http://code.google.com/p/apv/downloads/list
https://market.android.com/details?id=cx.hell.android.pdfview&feature=search_result
When you start activity with your given params, it searches for all applications/Activities/Intents that are registered to open pdf format. Since you have none in your device, you are getting the ActivityNotFoundException

First install the pdf reader on device.
than use this code for reading pdf file from internal memory.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView tv = (TextView)findViewById(R.id.tv);
Button bt=(Button)findViewById(R.id.openbtn);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File pdfFile = new File(Environment.getExternalStorageDirectory(),"Leave.pdf");
if(pdfFile.exists())
{
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){
tv.setText("No Application available to view PDF");
}
}
else
{
tv.setText("File not found");
}
}
});
}

Your code is right, I have also used same code to open pdf file within a viewer.
As you don't have a viewer installed to your device so it can't open without any viewer.
You can install Adobe reader for Android.
I can't open pdf file within emulator, so I have to test using my device.

Related

opening sdcard gallery album directory

Hi I am making the button which opens gallery(Default)album in sd card
putting following funtion to the button
But i don't understand why this funtion makes whole application stops...
public void goTopAlbum(View v){
Toast.makeText(MainActivity.this, "Let's open gallery!", Toast.LENGTH_SHORT).show();
Intent mIntent = new Intent(getIntent().ACTION_VIEW, Uri.parse("file:///sdcard/image/Album1"));
//dataType name = new dataType
startActivity(mIntent);
}
getIntent().ACTION_VIEW
Change to
Intent.ACTION_VIEW

sending data from one app to another app in android(AIR for ANDROID)

As we can send data from one Activity to another within the app and also from one app to another app by using Intent
I'm able to send data from my one app to another by using following code
just consider there are two apps APP1 and APP2 and I'm sending data from APP1 to APP2 and vice verse.
In APP1 package name: (com.sush.myApp)
public class HomeActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// to receive data from com.my.demo package and display it to TextView
TextView mText = (TextView)findViewById(R.id.textView1);
String name=getIntent().getStringExtra("User_Message");
mText.setText(name);
// to send data from com.sush.myApp package to com.my.demo package
Button btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
try
{
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.my.demo");
i.putExtra("User_ID", "sush19");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(v.getContext(), "App Not Found", Toast.LENGTH_LONG).show();
}
}
});
}
and from APP2 package name: (com.my.demo)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// to receive data from com.sush.myApp package and display it to TextView
final TextView txt1 = (TextView)findViewById(R.id.Text1);
String name=getIntent().getStringExtra("User_ID");
txt1.setText(name);
// to send data from com.my.demo package to com.sush.myApp package
Button btnSending = (Button)findViewById(R.id.btnSend);
final EditText myMessage = (EditText)findViewById(R.id.txtMessage);
btnSending.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (myMessage.getText().toString().equals(""))
{
Toast.makeText(v.getContext(), "Enter your message", Toast.LENGTH_SHORT).show();
}
else
{
try
{
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.sush.myApp");
i.putExtra("User_Message", myMessage.getText().toString());
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(v.getContext(), "Error: "+e, Toast.LENGTH_LONG).show();
}
}
}
});
}
And its working perfect..
Now my problem is I want to do same operation i.e sending data from one app to another but my first app is in Java created using Eclipse and my second app is in ActionScript created using AIR for Android in Adobe Flash Professional CS6
Is there a way to use Intent and PackageManager in Actionscript so that I can send the data easily from AIR app to Android App, if yes then can anyone show me a example
Or else can anyone show me an example on how to send data from Android App to AIR for Android App and vice verse..
Thank you...
There's 2 methods I can think of for this.
The first is to use the Java code you have written (or similar), package it as a Native Extension, and build that into your app.
Another alternative is to use a URI scheme and read the data from the InvokeEvent. This SO question covers that method already.

Open Epub from uri ActivityNotFoundException Error Android

Im open a epub file when I press a button, but when my android donĀ“t have any epub reader, show an toast saying please install epub reader, but my code not works, and stopp my app saying the app stop unexpectedly.
Button leer = (Button) findViewById(R.id.Btnleer);
leer.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v2) {
// TODO Auto-generated method stub
File file = new File("/sdcard/Mi Biblioteca/"+nomb+".epub");
Intent intentreadf = new Intent(Intent.ACTION_VIEW);
intentreadf.setDataAndType(Uri.fromFile(file),"application/epub+zip");
intentreadf.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intentreadf);
try {
startActivity(intentreadf);
}
catch (ActivityNotFoundException e) {
Toast.makeText(getApplicationContext(),
"favor descargar lector epub.",
Toast.LENGTH_SHORT).show();
}
}
});
Remove the first startActivity(intentreadf); command right after intentreadf.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);. It gets called before your try-catch block, and hence throws an exception.

Android - Show image from resource drawable

I'm new at Android apps and I'm working on a project started by a friend of mine. The code below is from a class that loads a view that has a small image with some text around it.
I want the image to appear fullscreen (on the standard Android image viewer) when I click on the thumbnail.
I tried several different solutions, and none of them worked.
This example throws the following exception:
E/AndroidRuntime(13768): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=android.resource://com.app.mid/2130837533 typ=image/png }
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_n1);
ImageView img = (ImageView) findViewById(imageResource);
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("android.resource://com.app.mid/" + R.drawable.p_page_11_image_0001), "image/png");
startActivity(intent);
}
});
}
The images are inside the drawable folder (..\res\drawable-xhdpi). I'm using Eclipse ADT and I configured it to run on my Galaxy Nexus.
Again, I tried several solutions with no luck. Any thoughts?
Thanks
Try to use Intent.ACTION_VIEW instead of android.content.Intent.ACTION_VIEW
Intent i = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(yourUri, "image/png");
startActivity(intent);

PDF viewer in online(Internet conectivity) as well as offline mode

The PDF content can be of online as well as offline mode, I need to show pdf in my own customized layout so, Intent can't be used. Any suggestion will be appreciated. thank you.
Yes we are able to show pdf content onLine with the help of google doc api Here i am given code for using it. for ON Line Mode
public class ReaderActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView=(WebView)this.findViewById(R.id.WebView01);
Intent intent = new Intent(Intent.ACTION_VIEW);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setPluginsEnabled(true);
webView.loadUrl("https://docs.google.com/viewer?url=http%3A%2F%2Fwww.eli.sdsu.edu%2Fcourses%2Ffall09%2Fcs696%2Fnotes%2FAndroid13Web.pdf");
//intent.setDataAndType(Uri.parse("https://docs.google.com/viewer?url=---------Your URL");
}}
for OFF Line Mode
File file = new File(mRealPath);
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setDataAndType(path, getString(R.string.application_type));
try
{
startActivity(intent);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(FirstTab.this,
getString(R.string.no_application_found),
Toast.LENGTH_SHORT).show();
}
Note-:In off line mode first you have download file form server to own local device in sd-card the get path of this document and put it on in the place of path variable then you get answer of your hole question.
In this code you should use your url in the place my url.
I hope this is help.
First of there is no support for pdf in Android so you need to open in some other app like adob or if you want to do it right way then need make the load lib like vudroid and apdfviewer .
apdfviewer is very good but there is no support how to compile source code, actually all lib work with c++ in backend so you need to install the ndk.
Vudroid is slow but you can compile easily.
I hope this will help you.
But
Some phones (like the Nexus One) come with a version of Quickoffice pre-installed so it may be as easy as sending the appropriate Intent once you've saved the file to the SD card.
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();
}
}
}
});
}
}
I found that Internet connectivity mode :- through google doc we can open the pdf.
but in the Internet offline mode:- we need to use Intent and open the pdf file through any one of the pdf viewer application install in the device.
one things we can do in the offline mode is that, instead of opening the option of multiple pdf viewer reader in device and select one application to open pdf file we can open the pdf with the particular application.

Categories

Resources