當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。