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;
}
Related
I need to check if a document (pdf,xls,docx,ppt..etc) is viewed by user or not. In some user's devices there are no apps to open some document types. In such cases I need to know that the document is not opened/viewed by user.
This is an android app developed using xamarin forms. Used below code to open document. This is working fine if there is an app to open respective document. But, if there are no apps to open the document it shows blank activity with the title "Choose an Application:"
...other code here...
var chooserIntent = Intent.CreateChooser(intent, "Choose an Application:");
activity.StartActivityForResult(chooserIntent, 10);
You can check it by the Android file MIME type.Refer the following code
Dictionary<string, string> matchArray = new Dictionary<string, string>();
matchArray.Add(".3gp", "video/3gpp");
matchArray.Add(".apk", "application/vnd.android.package-archive");
matchArray.Add(".doc", "application/msword");
matchArray.Add(".gif", "image/gif");
matchArray.Add(".wps", "application/vnd.ms-works");
matchArray.Add(".pdf", "application/pdf");
matchArray.Add(".ppt", "application/vnd.ms-powerpoint");
matchArray.Add(".png", "image/png");
//...
public void OpenFileByPath(Context context, string path)
{
if (context == null || path == null)
return;
string type = "";
foreach (string key in matchArray.Keys)
{
if (path.Contains(key))
{
type = matchArray[key];
break;
}
}
if (type == "")
{
//there is no app can open the file ,do some you want
}
else
{
// open your file
}
}
You can add more key-values about MIME type by searching from the internet
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
}
I wish I could open any file type using mobile applications.
Here is the code :
File mFile = new File( file.getLocalPath() + file.getFileName() );
Log.i("path", file.getLocalPath() + file.getFileName());
if( mFile.exists() )
{
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(mFile.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null)
type = "*/*";
Log.i("type", type);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
Uri data = Uri.fromFile(mFile);
Log.i("uri", data.toString());
intent.setDataAndType( data, type );
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException e) {
}
}
Logs :
Path : /data/data/com.package.name/files/sbx/523/filename.jpg
Type : image/jpeg
Uri : file:///data/data/com.package.name/files/sbx/523/filename.jpg
All logs are displayed therefore the file exists.
However, during the opening of the gallery, I get an "Unable to find item".
Where is the problem ? Are there other solutions ?
The problem is that you are trying to open file which is in android private area thats why during the opening of the gallery, you get an "Unable to find item". You have to put your file in local. If you want to open a file of android private area you should be rooted.
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. :)
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);
}