Reputation: 69
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();
}
Upvotes: 4
Views: 3050
Reputation: 31
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 );
Upvotes: 3
Reputation: 1966
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
Upvotes: 0
Reputation: 186
Try get mime type from file
public String getMimeTypeByFile(String filePath) {
MimeTypeMap type = MimeTypeMap.getSingleton();
return type.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(filePath));
}
Upvotes: 0
Reputation: 91
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!
Upvotes: 0