How to open a PDF via Intent from SD card
Try this code, display pdf file From /sdcard
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Android open pdf file via Intent
I think you forgot to specify the file. See your code, you only pointed it to the folder, but no the file itself. I think that is why it tells you that it is in wrong format, because it does not end with .pdf
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "SOMEFOLDER" + File.separator + "pdffile.pdf");
EDIT: Modify methods according to your comment
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
String fileName = FilesInFolder.get(position);
open_File(fileName);
}
});
public void open_File(String filename){
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/SOMEFOLDER", filename);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent1 = Intent.createChooser(intent, "Open With");
try {
startActivity(intent1);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
Open online pdf file through android intent?
You can view or download the pdf by either of the two ways i.e by opening it in device in-built browser or in the webview by embedding it in your app.
To open the pdf in browser,
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pdf_url));
startActivity(browserIntent);
Instead to open in webview,
WebView webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(pdf_url);
Android open pdf file
The problem is that there is no app installed to handle opening the PDF. You should use the Intent Chooser, like so:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
Android intent filer for opening pdf files
Try this
//Method to generate a MIME Type
private static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
//Method to generate File URI
private static Uri getFileUri(String filePath) {
Uri fileUri = null;
File file = new File(filePath);
try {
if (Build.VERSION.SDK_INT >= 24) {
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}
fileUri = Uri.fromFile(file);
} catch (Exception e) {
e.printStackTrace();
}
return fileUri;
}
//Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(getFileUri(file_path), getMimeType(file_path));
startActivity(intent);
Open a selected file (image, pdf, ...) programmatically from my Android Application?
Try the below code. I am using this code for opening a PDF file. You can use it for other files also.
File file = new File(Environment.getExternalStorageDirectory(),
"Report.pdf");
Uri path = Uri.fromFile(file);
Intent pdfOpenintent = new Intent(Intent.ACTION_VIEW);
pdfOpenintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenintent.setDataAndType(path, "application/pdf");
try {
startActivity(pdfOpenintent);
}
catch (ActivityNotFoundException e) {
}
If you want to open files, you can change the setDataAndType(path, "application/pdf")
. If you want to open different files with the same intent, you can use Intent.createChooser(intent, "Open in...");
. For more information, look at How to make an intent with multiple actions.
How can I open a PDF file via my Android app?
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.
Open PDF from within Android application using any installed reader
Wow I can't believe no one answered this! I have found the answers my-self. I am posting them to help others and save them the time it took me to figure it out.
Question 1. The files should be stored in the /res/raw folder (make the folder if it is not there)
Question 2. The path is simply ("/app_name/pdf_name") quick note use %20 for spaces in the apps name.
-Nick
Related Topics
Gradle's Dependency Cache May Be Corrupt (This Sometimes Occurs After a Network Connection Timeout.)
How to Solve the Install_Failed_Dexopt Error
Geocoder.Getfromlocation Throws Ioexception on Android Emulator
Startactivity() from Broadcastreceiver
Simulate Low Network Connectivity for Android
How to Create Socket Connection in Android
Android - Get Text Out of Webview
How to Use Multiple Mapactivities/Mapviews Per Android Application/Process
Emulator in Android Studio Doesn't Start
Lazy Download Images into Gridview
Full Screen Dialogfragment in Android
Redraw a Single Row in a Listview