Opening pdf file programmatically is going into menu page? - android

How come every time i try to open a pdf file in my SDCARD using the following code, it doesn't actually open the pdf file itself, but goes into the menu of adobe reader? Is there anything wrong with my code?
Intent intent = new Intent();
File pdfFile = new File("/sdcard/sample.pdf");
Uri path = Uri.fromFile(pdfFile);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(path);
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader");
startActivity(intent);

No, there's nothing wrong. You've set the type to pdf and specified the package as adobe.reader. What that does is fire off an intent to launch the pdf in adobe reader. There's no way to display a pdf directly in your app without using a library (or writing code yourself) to render PDFs and display them.
Note that if you only set the type and not the package, the system will find whatever app is available to display PDFs
EDIT
You can try something like
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File( filename );
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);
or
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);

Here I am giving my pdf file name. You give your file name.
private static String FILE = Environment.getExternalStorageDirectory().getPath()+"/TestPDF.pdf";
File file = new File(FILE);
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(this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}

Related

how to intstall mcpack in minecraft programatically android

i'm trying to send (install) my mcpack file using this code but is is not working, minecraft support this type of extenstion to install mods (.mcpack, .mcaddon, .mcworld, .mctemplate, .modpkg how we can define mimetypes for these extensions)
please go through this image i need exactly like this https://imgur.com/14rIBvN
try {
//File path = new File(getFilesDir(), "dl");
File file = new File(path);
PackageManager manager = this.getPackageManager();
// Get URI and MIME type of file
Intent share = new Intent(Intent.ACTION_SEND);
//share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
share.setPackage("com.mojang.minecraftpe");//package name of the app
startActivity(Intent.createChooser(share, "Select Minecraft to install"));
} catch (Exception e) {
Log.i(TAG, "App not found.. "+e.getMessage());
}
i did it!)
File file = new File(this.getExternalFilesDir(null), "1.mcpack");
Uri photoURI = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
// set the content type and data of the intent
intent.setDataAndType(photoURI, "application/octet-stream");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// start the intent as a new activity
startActivity(intent);
Open with minecraft and it will open and it will day download started and if it works it will say, If it does not work it will say, Minecraft onlu supports .mcpack, .mcworld, .mctemplate on pocket edition

How to show docx and pdf files?

What do I want to achieve? I want to choose pdf or doc file and show them on recyclerview. What have I done?
Firstly, I tried to start activity like below:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(Intent.createChooser(intent, "Open document"), DOCUMENT_REQUEST_CODE);
When this attached document is clicked, I do following:
Intent docIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("content://com.android.providers.downloads.documents/document/doc_name"));
docIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(docIntent);
Above method opens Google Docs to load document and it can open only docs located on Google Drive. There is black view when I try to open document located in device storage. URI of document located in device is : content://com.android.providers.downloads.documents/document/doc_name and URI of document located in Google Drive is like : content://com.google.android.apps.docs.storage/document/acc%3D1%3Bdoc%3D430
After some searching I changed click method to following:
File file = new File("content://com.android.providers.downloads.documents/document/doc_name"); // url here is string type
if (file.exists()) {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf"); //only pdf here
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
Toast.makeText(getContext(),
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
}
But in above method, exists() returns false. Note, that all url that I pass are string type. So, how can I solve my problem?

Automatically open Intent Chooser for the file Android

I want to open a file in android. What i want to do is if the file is of type Image then i want to open Intent Chooser which contains applications that can view the image, and if it is of video type, then open Intent Chooser with applications that can view videos. How can i achieve this?
I have found a solution. I am pasting it here so it may help other users.
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(path);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
intent.setDataAndType(Uri.fromFile(file), type);
context.startActivity(intent);
You should decide and know whether the file is video or image. You may do it by looking at the extension of the files.
After that you can open videos like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(result), "video/*");
startActivity(intent);
and images like this:
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(result), "image/*");
startActivity(intent);
Android system will open the Intent Chooser automatically.

Open PDF in Android

I would like to open a PDF file from my android application. I've searched how to do it in internet, and it seems very easy, but it doesn't work, at least in my mobile (Sony XPeria P).
File file = ....
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.fromFile (file));
intent.setType("application/pdf");
startActivity(intent);
When this code is executed, a window is opened asking to choose an application to show the PDF. When I choose the Adobe Reader, it's opened by no document is shown.
What I'm doing wrong?
Try this, Its working for me
//Method to open the pdf file in the PDF Viewer
public void OpenPDFFile() {
File pdfFile = new File(Environment.getExternalStorageDirectory(),"PdfFile.pdf");//File path
if (pdfFile.exists()) //Checking for the file is exist or not
{
Uri path = Uri.fromFile(pdfFile);
Intent objIntent = new Intent(Intent.ACTION_VIEW);
objIntent.setDataAndType(path, "application/pdf");
objIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
startActivity(objIntent);//Staring the pdf viewer
}
}
The setType("application/pdf") function removes all the previous data.
If the targetSdkVersion >= 24 we need to use file provider to get the Uri. See this post for details: https://stackoverflow.com/a/38858040/8192914
So, the final code would look something like this:
Uri pathUri = FileProvider.getUriForFile(getBaseContext(), context.getApplicationContext().getPackageName() + ".provider", finalFile);
Intent pdfViewerIntent = new Intent(Intent.ACTION_VIEW);
pdfViewerIntent.setDataAndType(pathUri, "application/pdf");
pdfViewerIntent.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP);
pdfViewerIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(pdfViewerIntent);

How to read a pdf file stored in sdcard using adobe reader installed in the android device?

i new to android application developemnt. I am trying to open the pdf file stored in sd card.I used this code snippet but this opens the viewer in the device but not the file given in the path. Any help appreciated.
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("Environment.getExternalStorageDirectory().getPath()+/sample.pdf"));
intent.setType("application/pdf");
intent.setPackage("com.adobe.reader"); selects the adobe reader directly
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent,0);
if (activities.size() >= 0)
{
startActivity(intent);
} else {
Toast.makeText(this, "No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
you can read pdf file from sdcard by giving the path and try the following.
File pdfFile = new File(path);
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(uractivity.this, resource.getString(R.string.noapplicationpdf), Toast.LENGTH_LONG).show();
}
}
You can try something like
Intent intent = new Intent(Intent.ACTION_VIEW);
File file = new File(filename);
intent.setDataAndType( Uri.fromFile( file ), "application/pdf" );
startActivity(intent);
or
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
startActivity(intent);

Categories

Resources