How to open pdf with HP ePrint application - android

I want to try open directly PDF file with HP ePrint application, but I am little confused what parameters I should send to this package name (com.hp.android.print), so I just click print button, instead of preview using Adobe Acrobat, then I get result of my print document (more effective)
This is my code, but default open application is using Adobe Acrobat
File dir=null;
if (filename !=null && !filename.isEmpty()){
dir = new File(Environment.getExternalStorageDirectory().toString()+"/Offer/"+
offerordernum.substring(0,10)+"/"+filename);
Uri uri = Uri.fromFile(dir);
Boolean isIntentSafe=false;
PackageManager pm = mycontext.getPackageManager();
try {
pm.getPackageInfo("com.adobe.reader",0);
Intent intent = new Intent();
intent.setPackage("com.adobe.reader");
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mycontext.startActivity(intent);
}catch (PackageManager.NameNotFoundException e) {
final String appPackageName = "com.adobe.reader"; // getPackageName() from Context or Activity object
try {
mycontext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
}catch (android.content.ActivityNotFoundException anfe) {
mycontext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}else {
}
I have already tried with changed packagename, but it was error run time. Could somebody help me to solve this problem ?
Here similar screenshot of HP ePrint document before running print

Related

How to open excel, doc files with intent from my app in MS-Excel or MS-Word - Android

My requirement is to download and view the Excel, Doc files in my app. So I downloaded the Excel/Doc file in phone storage and called the ACTION_VIEW Intent by passing the file path. Then I got this error saying "Try saving the file on the device and then opening it."
I can be glad if any one can suggest me another alternatives as well to open excel or doc files. Please guys i have searched a lot for this so far i didn't find the solution and i am sure that i have used proper intent for opening files.
Where My Code:
Intent intentpdf = new Intent(Intent.ACTION_VIEW);
intentpdf.setDataAndType(Uri.parse(message), "application/msword");
intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
mactivity.startActivity(intentpdf);
} catch (ActivityNotFoundException e) {
if (!mactivity.isFinishing())
Toast.makeText(mactivity, "Install MSWord Viewer.", Toast.LENGTH_LONG).show();
}
Intent intentpdf = new Intent(Intent.ACTION_VIEW);
intentpdf.setDataAndType(Uri.parse(message), "application/vnd.ms-excel");
intentpdf.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
mactivity.startActivity(intentpdf);
} catch (ActivityNotFoundException e) {
if (!mactivity.isFinishing())
Toast.makeText(mactivity, "Install Excel Viewer.", Toast.LENGTH_LONG).show();
}
You need the flags FLAG_ACTIVITY_NO_HISTORY,FLAG_GRANT_READ_URI_PERMISSION, FLAG_GRANT_WRITE_URI_PERMISSION.
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
I had this exact same problem and I fixed it. The problem is most likely with your ContentProvider/FileProvider implementation. My Excel sheet opened fine in Google Sheets but I got this error in MS Excel. The fix was to return the proper content type (via the getType() method) out of your provider. Returning the wrong content type will cause the error.
Try this code to see if it helps...and if it does, you can fully implement the routine for files of all types.
#Override
public String getType(Uri uri) {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
Good Luck!
Try get mime type from file
public String getMimeTypeByFile(String filePath) {
MimeTypeMap type = MimeTypeMap.getSingleton();
return type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filePath));
}
nothing is worked for me to open the Doc,PPT,Excel files with intent at last I find a better solution , please check the code here
if(fileType.equalsIgnoreCase("doc") || fileType.equalsIgnoreCase("docx")) {
String str = "com.microsoft.office.word";
Uri pathe = Uri.fromFile(fileN);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(pathe, "application/msword");
PackageManager packageManager = activity.getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
activity.startActivity(intent.createChooser(intent, "Choose app to open document"));
}
else
{
//Launch PlayStore
try {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+str)));
} catch (android.content.ActivityNotFoundException n) {
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id="+str)));
}
}
Here is the result : check the screenshot

Need to know about app chooser in android

I have a list of files in listview .. these files actually reside on sd card. Now i want to open these files by using an app picker. I.e if this file is an image it should show all applications in my phone that can open jpg type files in application chooser box.
How can i do this .. can someone give me any idea about it?
Any help is appreciated :)
Thanks in advance
I found dis piece of code ..how can i use it in my application?
public class Redirector {
public static void showActivityWithChooser( Context context, int chooserLabelTitleId, Intent intent ) {
try {
context.startActivity( Intent.createChooser( intent,
context.getResources().getString( chooserLabelTitleId )) );
} catch( Exception e ) {
e.printStackTrace();
}
}
public static void viewInExternalApplication( Context context, String url ) {
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setData( Uri.parse( url ) );
intent.addFlags( Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET );
showActivityWithChooser( context, R.string.open_chooser_title, intent );
}
}
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/YOUR_PATH_TO_Images/";
try {
if (f.exists()) {
File file = new File(path
+ listViewArray.get(position).getImageName());
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),
"image/*");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent1 = Intent.createChooser(target,
"Open With");
startActivity(intent1);
}
} catch (ActivityNotFoundException e) {
Toast.makeText(getActivity(), "No Pdf found",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
Just send the intent. Android will show an app chosser itself, if more than one app capable of showing the specified file type is installed, or start the app directly, if there's just one (or the user selected "always"/"remember my choice" in the app chooser)
To get the applications that can successfully open a given intent you'll use the PackageManager. Simply construct the intent as above and then use this code to get the applications that can handle the intent.
Intent myIntent = new Intent();
myIntent.setAction(Intent.ACTION_VIEW);
myIntent.addCategory("android.intent.category.LAUNCHER");
myIntent.setType("mp3");
PackageManager manager = getPackageManager();
List<ResolveInfo> info = manager.queryIntentActivities(myIntent,PackageManager.MATCH_DEFAULT_ONLY);
This will give you all the information on the programs that can handle the intent, including icon, and packagename. You can then create a dialog box with these options and save the option the user chooses.
1.This is question is related to Mime type
First get extenstion of file and set type of mime used by element into list
private String extenstionFile(String url) {
if (url.indexOf("?")>-1) {
url = url.substring(0,url.indexOf("?"));
}
if (url.lastIndexOf(".") == -1) {
return null;
} else {
String ext = url.substring(url.lastIndexOf(".") );
if (ext.indexOf("%")>-1) {
ext = ext.substring(0,ext.indexOf("%"));
}
if (ext.indexOf("/")>-1) {
ext = ext.substring(0,ext.indexOf("/"));
}
return ext.toLowerCase();
}
}
Than open supported type of application list:-
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);
//Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(extenstionFile(getFile().toString()).substring(1));
newIntent.setDataAndType(Uri.fromFile(getFile()),mimeType);
newIntent.setFlags(newIntent.FLAG_ACTIVITY_NEW_TASK);
try {
_context.startActivity(newIntent);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(_context, "No handler for this type of file.", 4000).show();
}
After selecting the file please check whic file is it (ex pdf , jpg) , After that create an ACTION_VIEW intent and set the type of intent according to selected file type. Now brocast the intent , System itself will show you the list of applications thats supports the viewing of yor file.
Check below link you will get better idea.
http://developer.android.com/guide/components/intents-filters.html

How to install apps from GooglePlay from Activity ? Android

I made a simple code that will download a app from Google Play.I tried
the code and tested it on real device but I got an error called "No application can perform this action."I declared android.permission.INTERNET in the manifest, but still doesn't work.
I would be glad if you can help me out.
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("market://developer?*/urlofgoogleplay*/"));
chooser = Intent.createChooser(i,"Launch Market");
startActivity(chooser);
public static void linkGooglePlay(Context context) {
Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
context.startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
context.startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse("http://play.google.com/store/apps/details?id=" + context.getPackageName())));
}
}
This is the right syntax:
i.setData(Uri.parse("market://details?id=" + app_package));
Here's the documentation

Open PDF-File in Android not working

I try to open a pdf file from my apps directory through a pdf viewer on the device.
PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p;
try {
p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (NameNotFoundException e) {
Log.w("Error", "Error Package not found ", e);
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s + "\\Document.pdf"));
intent.setType("application/pdf");
PackageManager pm = getPackageManager();
Intent crC = Intent.createChooser(intent, "Open File"); startActivity(crC);
On the test device is an pdf viewer installed. Nevertheless I'm told that no existing app is able to open that file. Am I doing something wrong?
If anybody is still interested, here is my solution:
I simply changed the code from this:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(s + "\\Document.pdf"));
intent.setType("application/pdf");
to this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(s + "\\Document.pdf"), "application/pdf");
And suddenly it worked. :)
What is the following code supposed to do?
PackageManager pm = getPackageManager();
Intent crC = Intent.createChooser(intent, "Open File"); startActivity(crC);
I assume you looked at a few tutorials and got a mixture of everything in here. Instead of those two lines, just try
startActivity(intent);
Does that not work?

Directly open up the Play store rating screen from our app in android

Is it possible to directly open up the rating screen dialog of play store app of a particular appllication?
Try this..
Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "Couldn't launch the market", Toast.LENGTH_LONG).show();
}
Or
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=Your Main Package Name"));
startActivity(intent);

Categories

Resources