How to open all any fomat files on Android - android

I try to implement a browser-like app.
I want to let it can open any format files which supported by device.
I know below code can open specific format:
String mimetype = mime_type(FileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(File), mimetype);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
public String mime_type(String name) {
String type = null;
String[] mime = {".htm\ttext/html", ".html\ttext/html", ".doc\tapplication/msword", ".ppt\tapplication/vnd.ms-powerpoint", ".xls\tapplication/vnd.ms-excel",
".txt\ttext/plain", ".pdf\tapplication/pdf", ".xlsx\tapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
".pptx\tapplication/vnd.openxmlformats-officedocument.presentationml.presentation", ".docx\tapplication/vnd.openxmlformats-officedocument.wordprocessingml.document"};
int i;
for(i = 0; i < mime.length; i++) {
if(name.toLowerCase().endsWith(mime[i].split("\t")[0])) {
return mime[i].split("\t")[1];
}
}
return type;
}
But the file formats are more than I can list.
If any methods to do it for all formats?
Or any methods to list all applications let user select?

I find set mimetype to */* can arrive it.
It will show all intalled app for select.

Related

Read file of any extension in android programmatically

Just want to confirm that is it possible that when i click on file of any extension will open up with its compatible software in android phone or display me the list of software’s present in mobile which can open the file and if it didn't found any software it will indicate user to first download the software to open that particular file (All this thing need to be done pro grammatically).
Thanks.
Any help will be appreciated.
In order to open the file you can use the following method, If there is no application that can handle given file, it simply shows a Toast saying no application found.
private void viewFile(String filePath, String title, int fileType) {
Uri uri = Uri.parse("file://" + filePath);
Intent intent = new Intent(Intent.ACTION_VIEW);
String dataAndType = getIntentDataAndType(filePath);
intent.setDataAndType(uri, dataAndType);
intent.putExtra(Intent.EXTRA_TITLE, title);
// Verify that the intent will resolve to an activity
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(getActivity(), "No Application found", Toast.LENGTH_SHORT).show();
}
}
UPDATED :
For finding the mime type of the file.
private String getIntentDataAndType(String filePath) {
String exten = "";
int i = filePath.lastIndexOf('.');
// If the index position is greater than zero then get the substring.
if (i > 0) {
exten = filePath.substring(i + 1);
}
String mimeType = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(exten);
mimeType = (mimeType == null) ? "*/*" : mimeType;
return mimeType;
}

Open files from own file explorer

I've coded my own file explorer and now I want to start to Implement capabilities to open various files. I have implemented Mime Types and all other things but how can I parse specific selected file to specific "viewer activity"? I know by using intents but how to receive it in that viewer app and use it. Many thanks guys :)
Something like this should work. Change file to whatever you want.
File file = new File(Environment.getExternalStorageDirectory(), "movie.mp4");
int index = file.getName().lastIndexOf(".") + 1;
String extension = null;
if (index > 0) {
extension = file.getName().substring(index);
}
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
Uri u = Uri.parse("file://" + file.getAbsolutePath());
String mimeType = null;
if (extension != null) {
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
if (mimeType == null) {
mimeType = "*/*";
}
intent.setDataAndType(u, mimeType);
try {
startActivity(Intent.createChooser(intent, "Open with..."));
} catch (ActivityNotFoundException e) {
// handle the exception
}

How to open .xlsx files using Intents in Android?

I am using the following Intent to open .xlsx file using an Intent Chooser. I have Kingsoft Offie and Polaris office at my disposal to open these files.
var calcIntent = new Intent ();
calcIntent.SetAction (Intent.ActionView);
Android.Net.Uri fileUri = Android.Net.Uri.FromFile (new File (OSUtils.GetCalcFilePath (id)));
calcIntent.SetData (fileUri);
var mimeType = OSUtils.GetMimeType (fileUri.ToString ());
calcIntent.SetType (mimeType);
try {
StartActivity (Intent.CreateChooser (calcIntent, "Open Via"));
} catch (ActivityNotFoundException) {
Toast.MakeText (this, "You do not have Kingsoft Office Installed!", ToastLength.Long).Show();
}
Where OSUtils.GetCalcFilePath is defined as
public static string GetCalcFilePath (int currentId) {
var calcDirPath = OSUtils.GetCalcDirForEstimate (currentId);
var calcSheetName = String.Format ("builder_calc_{0}.xlsx", currentId);
var calcSheet = new Java.IO.File (calcDirPath, calcSheetName);
if (!calcSheet.Exists ()) {
calcSheet.CreateNewFile ();
}
return calcSheet.Path;
}
and OSUtils.GetMimeType is defined as
public static string GetMimeType (string fileUri) {
String mimeType = null;
var extension = MimeTypeMap.GetFileExtensionFromUrl (fileUri);
if (extension != null) {
mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension (extension);
}
if (mimeType != null)
return mimeType;
else
return "*/*";
}
Now when I run it, I get a dialog(chooser) giving me two options, "Kingsoft Office" and "Polaris Office". Choosing Polaris Office gives me a toast saying "Not a supported document type" and on the other hand choosing Kingsoft Office just opens up the Kingsoft App and does nothing. The document is not opened in Kingsoft Office.
Whereas, if I go to my file manager and tap on the .xlsx file, it opens up perfectly in both the office apps. I checked my code and all the mimetypes and paths are correct and are pointing to the desired file. Any ideas?
Thanks in Advance
EDIT
It seems using SetData and SetType individually wont work here. They are mutually exclusive i.e calling one clears the other. "SetDataAndType" is the way to go here. :)
It seems using SetData and SetType individually wont work here. They are mutually exclusive i.e calling one clears the other.
"SetDataAndType" is the way to go here. :)

Document reader on Android

I want to write a read-only document reader.
The document is an online-document.
The file format contains *.doc, *.docx, *.ppt, *.pptx, *.xls, *.xlsx, *.pdf, *.txt.
Now I do it by download document to phone.
And use below code to open it.
But this method open file can be modify, I want to open with read-only.
File file = new File(TempFilePath);
String mimetype = mime_type(TempFileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), mimetype);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
public String mime_type(String name) {
String type = null;
String[] mime = {".htm\ttext/html", ".html\ttext/html", ".doc\tapplication/msword", ".ppt\tapplication/vnd.ms-powerpoint", ".xls\tapplication/vnd.ms-excel", ".txt\ttext/plain", ".pdf\tapplication/pdf", ".xlsx\tapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet", ".pptx\tapplication/vnd.openxmlformats-officedocument.presentationml.presentation", ".docx\tapplication/vnd.openxmlformats-officedocument.wordprocessingml.document"};
int i;
for(i = 0; i < mime.length; i++) {
if(name.toLowerCase().endsWith(mime[i].split("\t")[0])) {
return mime[i].split("\t")[1];
}
}
return type;
}
Because the document's URL and phone's wifi may in the same local network, so the GoogleDocs is not useful for me.
How can I do to read online document with read-only?
I think about that you should define permission in AndroidManifest.xml of Android.
use below permission:
android.permission.WRITE_EXTERNAL_STORAGE
android.permission.READ_PHONE_STATE

android and itext

I want to display pdf in android with next/previous and zoom effect,i check out the already exits the lib for that but it is too slow then, I move to itext.
I install the jar file and code to get the total number pages.
But question how i can display the pdf,i mean to say take the text view,then how i will manage the background of the pdf,so there are many query.
Can anybody help me out if some-one have gone through the steps
Thanks in advance and every suggestion wel come here.
As far as I know iText is not meant to display PDF files, but to manipulate (as in: read, change, write) them. Viewing is not possible.
try it:
private void openFile(String path) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
File f = new File(path);
String type = FileUtil.getMIMEType(f.getName());
intent.setDataAndType(Uri.fromFile(f), type);
startActivity(intent);
}
public static String getMIMEType(String name) {
String type = "";
String end = name.substring(name.lastIndexOf(".") + 1, name.length()).toLowerCase();
end = end.toLowerCase();
if (end.equals("apk")) {
return "application/vnd.android.package-archive";
} else if (end.equals("mp4") || end.equals("avi") || end.equals("3gp")
|| end.equals("rmvb")) {
type = "video";
} else if (end.equals("m4a") || end.equals("mp3") || end.equals("mid") || end.equals("xmf")
|| end.equals("ogg") || end.equals("wav")) {
type = "audio";
} else if (end.equals("jpg") || end.equals("gif") || end.equals("png")
|| end.equals("jpeg") || end.equals("bmp")) {
type = "image";
} else if (end.equals("txt") || end.equals("log") || end.equals("sql")) {
type = "text";
}else {
type = "*";
}
type += "/*";
return type;
}
Install a pdf viewer such as Adobe Reader onto your device (it is available onn Android MarketPlace). Then from use this function in your app you can use an intent to open the pdf
public void OpenPDF(File f)
{
Uri uri = Uri.parse("file://" + file.getAbsolutePath());
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}

Categories

Resources