forked from markeeftb/FileOpener
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileOpener.java
127 lines (110 loc) · 4.56 KB
/
FileOpener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* PhoneGap is available under *either* the terms of the modified BSD license *or* the
* MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
*
* Copyright (c) 2005-2010, Nitobi Software Inc.
* Copyright (c) 2011, IBM Corporation
*/
package com.phonegap.plugins.fileopener;
import java.io.IOException;
import java.net.URLConnection;
import org.apache.cordova.api.CallbackContext;
import org.json.JSONArray;
import org.json.JSONException;
import android.content.Intent;
import android.net.Uri;
import org.apache.cordova.api.CordovaPlugin;
public class FileOpener extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
try {
if (action.equals("openFile")) {
if( openFile(args.getString(0)) ) {
callbackContext.success();
}
else {
callbackContext.error("Could not open file");
}
return true;
}
} catch (IOException e) {
e.printStackTrace();
callbackContext.error(e.getMessage());
} catch (RuntimeException e) { // KLUDGE for Activity Not Found
e.printStackTrace();
callbackContext.error(e.getMessage());
}
return false;
}
private boolean openFile(String url) throws IOException {
// Create URI
Uri uri = Uri.parse(url);
Intent intent;
// Check what kind of file you are trying to open, by comparing the url with extensions.
// When the if condition is matched, plugin sets the correct intent (mime) type,
// so Android knew what application to use to open the file
if (url.contains(".doc") || url.contains(".docx")) {
// Word document
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/msword");
} else if(url.contains(".pdf")) {
// PDF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
} else if(url.contains(".ppt") || url.contains(".pptx")) {
// Powerpoint file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.ms-powerpoint");
} else if(url.contains(".xls") || url.contains(".xlsx")) {
// Excel file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.ms-excel");
} else if(url.contains(".rtf")) {
// RTF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/rtf");
} else if(url.contains(".wav")) {
// WAV audio file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "audio/x-wav");
} else if(url.contains(".gif")) {
// GIF file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/gif");
} else if(url.contains(".jpg") || url.contains(".jpeg")) {
// JPG file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "image/jpeg");
} else if(url.contains(".txt")) {
// Text file
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "text/plain");
} else if(url.contains(".mpg") || url.contains(".mpeg") || url.contains(".mpe") || url.contains(".mp4") || url.contains(".avi")) {
// Video files
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "video/*");
}
//if you want you can also define the intent type for any other file
//additionally use else clause below, to manage other unknown extensions
//in this case, Android will show all applications installed on the device
//so you can choose which application to use
// else {
// intent = new Intent(Intent.ACTION_VIEW);
// intent.setDataAndType(uri, "*/*");
// }
else {
String mimeType = URLConnection.guessContentTypeFromName(url);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, mimeType);
}
try
{
this.cordova.getActivity().startActivity(intent);
return true;
}
catch (Exception e)
{
return false;
}
}
}