Hello Guys,
Here I share an example of Read PDF using Adobe Reader. It gives you to bookmark selected page, add comment and do more settings from adobe reader.
Sample Code:
File: MainActivity.java
package com.sneha.pdfreader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity
{
URLConnection urlConnection = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new Thread(new Runnable()
{
public void run()
{
downloadFile();
}
}).start();
}
void downloadFile()
{
// TODO Auto-generated method stub
try{
URL url = new URL("http://www.tutorialspoint.com/android/android_tutorial.pdf");
//Opening connection of currrent url
urlConnection = url.openConnection();
urlConnection.connect();
String PATH = Environment.getExternalStorageDirectory() + "/MyDownloads/";
final File file = new File(PATH);
file.mkdirs();
final File outputFile = new File(file, "android_tutorial.pdf");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = url.openStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
runOnUiThread(new Runnable()
{
public void run()
{
Uri path = Uri.fromFile(outputFile);
Log.i("Log_Path", "Path Is: "+path);
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getApplicationContext(),"PDF Reader application is not installed in your device",Toast.LENGTH_SHORT).show();
}
}
});
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
}
File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sneha.pdfreader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
Here I share an example of Read PDF using Adobe Reader. It gives you to bookmark selected page, add comment and do more settings from adobe reader.
Sample Code:
File: MainActivity.java
package com.sneha.pdfreader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;
public class MainActivity extends Activity
{
URLConnection urlConnection = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
new Thread(new Runnable()
{
public void run()
{
downloadFile();
}
}).start();
}
void downloadFile()
{
// TODO Auto-generated method stub
try{
URL url = new URL("http://www.tutorialspoint.com/android/android_tutorial.pdf");
//Opening connection of currrent url
urlConnection = url.openConnection();
urlConnection.connect();
String PATH = Environment.getExternalStorageDirectory() + "/MyDownloads/";
final File file = new File(PATH);
file.mkdirs();
final File outputFile = new File(file, "android_tutorial.pdf");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = url.openStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
runOnUiThread(new Runnable()
{
public void run()
{
Uri path = Uri.fromFile(outputFile);
Log.i("Log_Path", "Path Is: "+path);
try
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
catch (ActivityNotFoundException e)
{
Toast.makeText(getApplicationContext(),"PDF Reader application is not installed in your device",Toast.LENGTH_SHORT).show();
}
}
});
}
catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
}
}
}
File: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sneha.pdfreader"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
Happy Coding...!!!
No comments:
Post a Comment