当前位置: 首页>>代码示例>>Java>>正文


Java MimeTypeMap类代码示例

本文整理汇总了Java中android.webkit.MimeTypeMap的典型用法代码示例。如果您正苦于以下问题:Java MimeTypeMap类的具体用法?Java MimeTypeMap怎么用?Java MimeTypeMap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


MimeTypeMap类属于android.webkit包,在下文中一共展示了MimeTypeMap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getMimeType

import android.webkit.MimeTypeMap; //导入依赖的package包/类
/**
 * To find out the extension of required object in given uri
 * Solution by http://stackoverflow.com/a/36514823/1171484
 */
public static String getMimeType(Activity context, Uri uri) {
    String extension;
    //Check uri format to avoid null
    if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        //If scheme is a content
        extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri));
        if (TextUtils.isEmpty(extension))extension=MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
    } 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());
        if (TextUtils.isEmpty(extension))extension=MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri));
    }
    if(TextUtils.isEmpty(extension)){
        extension=getMimeTypeByFileName(TUriParse.getFileWithUri(uri,context).getName());
    }
    return extension;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:23,代码来源:TImageFiles.java

示例2: getMimeTypeFromPath

import android.webkit.MimeTypeMap; //导入依赖的package包/类
private String getMimeTypeFromPath(String path) {
    String extension = path;
    int lastDot = extension.lastIndexOf('.');
    if (lastDot != -1) {
        extension = extension.substring(lastDot + 1);
    }
    // Convert the URI string to lower case to ensure compatibility with MimeTypeMap (see CB-2185).
    extension = extension.toLowerCase(Locale.getDefault());
    if (extension.equals("3ga")) {
        return "audio/3gpp";
    } else if (extension.equals("js")) {
        // Missing from the map :(.
        return "text/javascript";
    }
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
 
开发者ID:disit,项目名称:siiMobilityAppKit,代码行数:17,代码来源:CordovaResourceApi.java

示例3: 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);
}
 
开发者ID:Dnet3,项目名称:CustomAndroidOneSheeld,代码行数:39,代码来源:DataLoggerShield.java

示例4: resultReturned

import android.webkit.MimeTypeMap; //导入依赖的package包/类
/**
 * Callback method to get the result returned by the image picker activity
 *
 * @param requestCode a code identifying the request.
 * @param resultCode a code specifying success or failure of the activity
 * @param data the returned data, in this case an Intent whose data field
 *        contains the image's content URI.
 */
public void resultReturned(int requestCode, int resultCode, Intent data) {
  if (requestCode == this.requestCode && resultCode == Activity.RESULT_OK) {
    Uri selectedImage = data.getData();
    selectionURI = selectedImage.toString();
    Log.i(LOG_TAG, "selectionURI = " + selectionURI);

    // get the file type extension from the intent data Uri
    ContentResolver cR = container.$context().getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String extension = "." + mime.getExtensionFromMimeType(cR.getType(selectedImage));
    Log.i(LOG_TAG, "extension = " + extension);

    // save the image to a temp file in external storage, using a name
    // that includes the extension
    saveSelectedImageToExternalStorage(extension);
    AfterPicking();
  }
}
 
开发者ID:mit-cml,项目名称:appinventor-extensions,代码行数:27,代码来源:ImagePicker.java

示例5: getOpenableIntent

import android.webkit.MimeTypeMap; //导入依赖的package包/类
/**
    * Method that return openable intent according to the file type
    *
    * @param file
    * @return
    */

public Intent getOpenableIntent(String file){
	if(isExists(currentDir+"/"+file) && new File(currentDir+"/"+file)
               .isFile()) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		Uri uri = Uri.fromFile(new File(currentDir+"/"+file));

		String type=MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap
				.getFileExtensionFromUrl(uri.toString()));
		intent.setDataAndType(uri,type);

		return intent;
	}
	else
		return null;
}
 
开发者ID:1singhmanmeet,项目名称:FileEx,代码行数:23,代码来源:FileEx.java

示例6: 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;
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:20,代码来源:FileUtil.java

示例7: getMimeTypeForEntry

import android.webkit.MimeTypeMap; //导入依赖的package包/类
private String getMimeTypeForEntry(ZipEntry entry) {
    if (entry.isDirectory()) {
        return Document.MIME_TYPE_DIR;
    }

    final int lastDot = entry.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = entry.getName().substring(lastDot + 1).toLowerCase(Locale.US);
        final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mimeType != null) {
            return mimeType;
        }
    }

    return BASIC_MIME_TYPE;
}
 
开发者ID:kranthi0987,项目名称:easyfilemanager,代码行数:17,代码来源:DocumentArchive.java

示例8: 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;
}
 
开发者ID:GlennioTech,项目名称:MetadataEditor,代码行数:21,代码来源:MimeTypes.java

示例9: createFile

import android.webkit.MimeTypeMap; //导入依赖的package包/类
@Override
public DocumentFileCompat createFile(String mimeType, String displayName) {
    // Tack on extension when valid MIME type provided
    final String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
    if (extension != null) {
        displayName += "." + extension;
    }
    final File target = new File(mFile, displayName);
    try {
        target.createNewFile();
        return new RawDocumentFile(this, target);
    } catch (IOException e) {
        Log.w(TAG, "Failed to createFile: " + e);
        return null;
    }
}
 
开发者ID:commonsguy,项目名称:cwac-document,代码行数:17,代码来源:RawDocumentFile.java

示例10: openFile

import android.webkit.MimeTypeMap; //导入依赖的package包/类
private void openFile(String filePath) {
    Uri uri = FileProvider.getUriForFile(getActivity(),
            BuildConfig.APPLICATION_ID + ".provider",
            new File(filePath));

    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension
            (fileExtension);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mimeType);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    startActivity(Intent.createChooser(intent, getString(R.string.open_file)));
}
 
开发者ID:metarhia,项目名称:metacom-android,代码行数:15,代码来源:ChatFragment.java

示例11: recordSaveLinkTypes

import android.webkit.MimeTypeMap; //导入依赖的package包/类
/**
 * Records the content types when user downloads the file by long pressing the
 * save link context menu option.
 */
static void recordSaveLinkTypes(String url) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    int mimeType = TYPE_UNKNOWN;
    if (extension != null) {
        String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (type != null) {
            if (type.startsWith("text")) {
                mimeType = TYPE_TEXT;
            } else if (type.startsWith("image")) {
                mimeType = TYPE_IMAGE;
            } else if (type.startsWith("audio")) {
                mimeType = TYPE_AUDIO;
            } else if (type.startsWith("video")) {
                mimeType = TYPE_VIDEO;
            } else if (type.equals("application/pdf")) {
                mimeType = TYPE_PDF;
            }
        }
    }
    RecordHistogram.recordEnumeratedHistogram(
            "ContextMenu.SaveLinkType", mimeType, NUM_TYPES);
}
 
开发者ID:rkshuai,项目名称:chromium-for-android-56-debug-video,代码行数:27,代码来源:ChromeContextMenuPopulator.java

示例12: remapGenericMimeType

import android.webkit.MimeTypeMap; //导入依赖的package包/类
/**
 * If the given MIME type is null, or one of the "generic" types (text/plain
 * or application/octet-stream) map it to a type that Android can deal with.
 * If the given type is not generic, return it unchanged.
 *
 * We have to implement this ourselves as
 * MimeTypeMap.remapGenericMimeType() is not public.
 * See http://crbug.com/407829.
 *
 * @param mimeType MIME type provided by the server.
 * @param url URL of the data being loaded.
 * @param filename file name obtained from content disposition header
 * @return The MIME type that should be used for this data.
 */
static String remapGenericMimeType(String mimeType, String url, String filename) {
    // If we have one of "generic" MIME types, try to deduce
    // the right MIME type from the file extension (if any):
    if (mimeType == null || mimeType.isEmpty() || "text/plain".equals(mimeType)
            || "application/octet-stream".equals(mimeType)
            || "binary/octet-stream".equals(mimeType)
            || "octet/stream".equals(mimeType)
            || "application/force-download".equals(mimeType)
            || "application/unknown".equals(mimeType)) {

        String extension = getFileExtension(url, filename);
        String newMimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (newMimeType != null) {
            mimeType = newMimeType;
        } else if (extension.equals("dm")) {
            mimeType = OMADownloadHandler.OMA_DRM_MESSAGE_MIME;
        } else if (extension.equals("dd")) {
            mimeType = OMADownloadHandler.OMA_DOWNLOAD_DESCRIPTOR_MIME;
        }
    }
    return mimeType;
}
 
开发者ID:rkshuai,项目名称:chromium-for-android-56-debug-video,代码行数:37,代码来源:ChromeDownloadDelegate.java

示例13: 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;
}
 
开发者ID:mkulesh,项目名称:microMathematics,代码行数:19,代码来源:FileUtils.java

示例14: testQueryActionViewIntentActivities

import android.webkit.MimeTypeMap; //导入依赖的package包/类
@Test
public void testQueryActionViewIntentActivities() throws Exception {
    File txt = new File("/test.txt");
    Uri uri = Uri.fromFile(txt);
    // 获取扩展名
    String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
    // 获取MimeType
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    // 创建隐式Intent
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mimeType);

    Context context = InstrumentationRegistry.getContext();
    PackageManager packageManager = context.getPackageManager();
    // 根据Intent查询匹配的Activity列表
    List<ResolveInfo> resolvers =
            packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

    for (ResolveInfo resolver : resolvers) {
        Log.d(TAG, resolver.activityInfo.packageName + "\n" + resolver.activityInfo.name);
    }
}
 
开发者ID:JulianAndroid,项目名称:AppChooser,代码行数:23,代码来源:ResolversRepositoryTest.java

示例15: 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;
}
 
开发者ID:tranleduy2000,项目名称:javaide,代码行数:23,代码来源:RootUtils.java


注:本文中的android.webkit.MimeTypeMap类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。