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.
Select pdf file from phone on button click and display its file name on textview
use startActivityForResult(intent, 1212) at the place of startActivity(intent); and do the foll0wing in onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1212:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String uriString = uri.toString();
File myFile = new File(uriString);
String path = myFile.getAbsolutePath();
String displayName = null;
if (uriString.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
} else if (uriString.startsWith("file://")) {
displayName = myFile.getName();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
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
}
Pick Image and Pdf using single intent
here just an example:
private Intent getFileChooserIntent() {
String[] mimeTypes = {"image/*", "application/pdf"};
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
intent.setType(mimeTypes.length == 1 ? mimeTypes[0] : "*/*");
if (mimeTypes.length > 0) {
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
}
} else {
String mimeTypesStr = "";
for (String mimeType : mimeTypes) {
mimeTypesStr += mimeType + "|";
}
intent.setType(mimeTypesStr.substring(0, mimeTypesStr.length() - 1));
}
return 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
}
Related Topics
Having Issue on Real Device Using Vector Image in Android. Svg-Android
Android: Changing Nfc Settings (On/Off) Programmatically
Android Support Library Error After Updating to 23.3.0
Switching Between Fragment View
Android Listview: Get Data Index of Visible Item
How to Resize a Android Webview After Adding Data in It
How to Change Background Image of Button When Clicked/Focused
Android Device Is Not Connected to Usb for Debugging (Android Studio)
Android 4.4 Giving Err_Cache_Miss Error in Onreceivederror for Webview Back
Using an Android Library Project Activity Within Another Project
Get Latitude and Longitude with Geocoder and Android Google Maps API V2
Android:Load Svg File from Web and Show It on Image View
Android Shape Border with Gradient
Android Webview VS Phone Browser
Camera in Android, How to Get Best Size, Preview Size, Picture Size, View Size, Image Distorted
Opencv Image Comparison in Android
How to Handle System_Alert_Window Permission Not Being Auto-Granted on Some Pre-Marshmallow Devices