本文整理汇总了Java中android.webkit.MimeTypeMap.getSingleton方法的典型用法代码示例。如果您正苦于以下问题:Java MimeTypeMap.getSingleton方法的具体用法?Java MimeTypeMap.getSingleton怎么用?Java MimeTypeMap.getSingleton使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.webkit.MimeTypeMap
的用法示例。
在下文中一共展示了MimeTypeMap.getSingleton方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getMimeType
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
public static String getMimeType(String filePath) {
if (TextUtils.isEmpty(filePath)) {
return "";
}
String type = null;
String extension = getExtensionName(filePath.toLowerCase());
if (!TextUtils.isEmpty(extension)) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
Log.i(TAG, "url:" + filePath + " " + "type:" + type);
// FIXME
if (StringUtil.isEmpty(type) && filePath.endsWith("aac")) {
type = "audio/aac";
}
return type;
}
示例2: openFile
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
/**
* 打开文件
* @param downloadPath
*/
@ReactMethod
private void openFile(String downloadPath){
File file = new File(downloadPath);
if (!file.exists()) return;
Intent intent = new Intent(Intent.ACTION_VIEW);
String url = "file://" + file.toString();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//设置intent的Action属性
intent.setAction(Intent.ACTION_VIEW);
//设置intent的data和Type属性。
intent.setDataAndType(Uri.parse(url), mimeString);
//跳转
mApplicationContext.startActivity(intent);
//这里最好try一下,有可能会报错。
//比如说你的MIME类型是打开邮箱,但是你手机里面没装邮箱客户端,就会报错。
}
示例3: getMimeType
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
private static String getMimeType(Context context, Uri uri) {
String extension;
//Check uri format to avoid null
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//If scheme is a content
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
//If scheme is a File
//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
extension = MimeTypeMap
.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}
return extension;
}
示例4: getMimeByExt
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
public final static String getMimeByExt(String ext, String defValue)
{
if (str(ext))
{
String[] descr = getTypeDescrByExt(ext);
if (descr != null)
return descr[1];
// ask the system
MimeTypeMap mime_map = MimeTypeMap.getSingleton();
if (mime_map != null)
{
String mime = mime_map.getMimeTypeFromExtension(ext.substring(1));
if (str(mime))
return mime;
}
}
return defValue;
}
示例5: openApk
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
private static boolean openApk(Context context, File file) {
try {
Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
} else {
uri = Uri.fromFile(file);
}
//create intent open file
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent intent = new Intent(Intent.ACTION_VIEW);
String ext = FileUtils.fileExt(file.getPath());
String mimeType = myMime.getMimeTypeFromExtension(ext != null ? ext : "");
intent.setDataAndType(uri, mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
return true;
}
示例6: showNotification
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
protected void showNotification(String notificationText) {
// TODO Auto-generated method stub
NotificationCompat.Builder build = new NotificationCompat.Builder(
activity);
build.setSmallIcon(OneSheeldApplication.getNotificationIcon());
build.setContentTitle(activity.getString(R.string.data_logger_shield_name));
build.setContentText(notificationText);
build.setTicker(notificationText);
build.setWhen(System.currentTimeMillis());
build.setAutoCancel(true);
Toast.makeText(activity, notificationText, Toast.LENGTH_SHORT).show();
Vibrator v = (Vibrator) activity
.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(1000);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeFileType = mimeTypeMap.getMimeTypeFromExtension("csv");
if(Build.VERSION.SDK_INT>=24) {
Uri fileURI = FileProvider.getUriForFile(activity,
BuildConfig.APPLICATION_ID + ".provider",
new File(filePath == null || filePath.length() == 0 ? "" : filePath));
notificationIntent.setDataAndType(fileURI, mimeFileType);
}
else{
notificationIntent.setDataAndType(Uri.fromFile(new File(filePath == null
|| filePath.length() == 0 ? "" : filePath)), mimeFileType);
}
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
PendingIntent intent = PendingIntent.getActivity(activity, 0,
notificationIntent, 0);
build.setContentIntent(intent);
Notification notification = build.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) activity
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify((int) new Date().getTime(), notification);
}
示例7: getMimeType
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
static String getMimeType(File file) {
if (file.isDirectory()) {
return null;
}
String type = "*/*";
final String extension = getExtension(file.getName());
if (extension != null && !extension.isEmpty()) {
final String extensionLowerCase = extension.toLowerCase(Locale
.getDefault());
final MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extensionLowerCase);
if (type == null) {
type = MIME_TYPES.get(extensionLowerCase);
}
}
if (type == null) type = "*/*";
return type;
}
示例8: openFileInSystem
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
public static void openFileInSystem(String path, Context context) {
try {
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(fileExt(path).substring(1));
newIntent.setDataAndType(Uri.fromFile(new File(path)), mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(newIntent);
} catch (Exception e) {
Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show();
}
}
示例9: listFiles
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
File[] listFiles(@Nullable String mimeType, @Nullable String[] extensions) {
File[] contents = parentFolder.listFiles();
List<File> results = new ArrayList<>();
if (contents != null) {
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
for (File fi : contents) {
if (fi.isDirectory()) {
results.add(fi);
} else {
if (extensions != null) {
boolean found = false;
for (String ext : extensions) {
if (fi.getName().toLowerCase().endsWith(ext.toLowerCase())) {
found = true;
break;
}
}
if (found) results.add(fi);
} else if (mimeType != null) {
if (fileIsMimeType(fi, mimeType, mimeTypeMap)) {
results.add(fi);
}
}
}
}
Collections.sort(results, new FileSorter());
return results.toArray(new File[results.size()]);
}
return null;
}
示例10: downloadApk
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public void downloadApk(String requestUrl, String dir, String filename)
{
Uri resource = Uri.parse(requestUrl);
DownloadManager.Request request = new DownloadManager.Request(resource);
request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI);
request.setAllowedOverRoaming(false);
//设置文件类型
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(requestUrl));
request.setMimeType(mimeString);
//在通知栏中显示
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.GINGERBREAD)
{
request.setShowRunningNotification(true);
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
{
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
// 显示下载界面
request.setVisibleInDownloadsUi(true);
//sdcard的目录下的download文件夹 /**AppConfig.DIR_APK*/
request.setDestinationInExternalPublicDir("/download/", filename + ".apk");
request.setTitle(filename + "");
long id = mDownloadManager.enqueue(request);
mTask.put(id, filename + "");
}
示例11: getCategoryByExt
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
public final static String getCategoryByExt(String ext)
{
if (str(ext))
{
String[] descr = getTypeDescrByExt(ext);
if (descr != null)
return descr[2];
// ask the system
MimeTypeMap mime_map = MimeTypeMap.getSingleton();
if (mime_map != null)
{
String mime = mime_map.getMimeTypeFromExtension(ext.substring(1));
if (str(mime))
{
String type = mime.substring(0, mime.indexOf('/'));
if (type.compareTo("text") == 0)
return C_TEXT;
if (type.compareTo("image") == 0)
return C_IMAGE;
if (type.compareTo("audio") == 0)
return C_AUDIO;
if (type.compareTo("video") == 0)
return C_VIDEO;
if (type.compareTo("application") == 0)
return C_APP;
}
}
}
return C_UNKNOWN;
}
示例12: shareFile
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
static void shareFile(Activity activity, String url) {
try {
File myFile = new File(url);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
activity.startActivity(Intent.createChooser(sharingIntent, activity.getString(R.string.shareWith)));
} catch (Exception e) {
e.printStackTrace();
}
}
示例13: getMimeType
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
/**
* 获取文件的MIME类型
*
* @param pathOrUrl the path or url
* @return mime type
*/
public static String getMimeType(String pathOrUrl) {
String ext = getExtension(pathOrUrl);
MimeTypeMap map = MimeTypeMap.getSingleton();
String mimeType;
if (map.hasExtension(ext)) {
mimeType = map.getMimeTypeFromExtension(ext);
} else {
mimeType = "*/*";
}
LogUtils.debug(pathOrUrl + ": " + mimeType);
return mimeType;
}
示例14: openFileByAnotherApp
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
private boolean openFileByAnotherApp(File file) {
//don't open compiled file
if (FileUtils.hasExtension(file, "class", "dex", "jar")) {
Toast.makeText(this, "Unable to open file", Toast.LENGTH_SHORT).show();
return false;
}
try {
Uri uri;
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(this, getPackageName() + ".provider", file);
} else {
uri = Uri.fromFile(file);
}
//create intent open file
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent intent = new Intent(Intent.ACTION_VIEW);
String ext = FileUtils.fileExt(file.getPath());
String mimeType = myMime.getMimeTypeFromExtension(ext != null ? ext : "");
intent.setDataAndType(uri, mimeType);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
return true;
}
示例15: shareFile
import android.webkit.MimeTypeMap; //导入方法依赖的package包/类
public static void shareFile(Context context, String url) {
try {
File myFile = new File(url);
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = myFile.getName().substring(myFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
Intent sharingIntent = new Intent("android.intent.action.SEND");
sharingIntent.setType(type);
sharingIntent.putExtra("android.intent.extra.STREAM", Uri.fromFile(myFile));
context.startActivity(Intent.createChooser(sharingIntent, "Share using"));
} catch (Exception e) {
e.printStackTrace();
}
}