Java Code Examples for android.webkit.MimeTypeMap

The following examples show how to use android.webkit.MimeTypeMap. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: secrecy   Source File: LocalStorageProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example 2
Source Project: DeviceConnect-Android   Source File: FileProvider.java    License: MIT License 6 votes vote down vote up
/**
 * ファイル名からMIMEタイプ取得.
 *
 * @param file ファイル
 * @return MIMEタイプ
 */
private String getMIMEType(final File file) {
    // 拡張子を取得
    String fileName = file.getName();
    int pos = fileName.lastIndexOf(".");
    String ext = (pos >= 0) ? fileName.substring(pos + 1) : null;
    if (ext != null) {
        // 小文字に変換
        ext = ext.toLowerCase(Locale.getDefault());
        // MIME Typeを返す
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    } else {
        // 拡張子が見つからない
        return null;
    }
}
 
Example 3
Source Project: attach   Source File: FileProvider.java    License: GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the MIME type of a content URI returned by
 * {@link #getUriForFile(Context, String, File) getUriForFile()}.
 *
 * @param uri A content URI returned by
 * {@link #getUriForFile(Context, String, File) getUriForFile()}.
 * @return If the associated file has an extension, the MIME type associated with that
 * extension; otherwise <code>application/octet-stream</code>.
 */
@Override
public String getType(Uri uri) {
    // ContentProvider has already checked granted permissions
    final File file = mStrategy.getFileForUri(uri);

    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }

    return "application/octet-stream";
}
 
Example 4
Source Project: cordova-plugin-intent   Source File: CordovaResourceApi.java    License: MIT License 6 votes vote down vote up
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);
}
 
Example 5
Source Project: FireFiles   Source File: DocumentArchive.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 6
Source Project: react-native-background-upload   Source File: UploaderModule.java    License: MIT License 6 votes vote down vote up
@ReactMethod
public void getFileInfo(String path, final Promise promise) {
  try {
    WritableMap params = Arguments.createMap();
    File fileInfo = new File(path);
    params.putString("name", fileInfo.getName());
    if (!fileInfo.exists() || !fileInfo.isFile())
    {
      params.putBoolean("exists", false);
    }
    else
    {
      params.putBoolean("exists", true);
      params.putString("size",Long.toString(fileInfo.length())); //use string form of long because there is no putLong and converting to int results in a max size of 17.2 gb, which could happen.  Javascript will need to convert it to a number
      String extension = MimeTypeMap.getFileExtensionFromUrl(path);
      params.putString("extension",extension);
      String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
      params.putString("mimeType", mimeType);
    }

    promise.resolve(params);
  } catch (Exception exc) {
    Log.e(TAG, exc.getMessage(), exc);
    promise.reject(exc);
  }
}
 
Example 7
Source Project: L.TileLayer.Cordova   Source File: CordovaResourceApi.java    License: MIT License 6 votes vote down vote up
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);
}
 
Example 8
Source Project: reader   Source File: CordovaResourceApi.java    License: MIT License 6 votes vote down vote up
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);
}
 
Example 9
@Override
public boolean shouldOverrideUrlLoading(XWalkView view, String url) {
    if (!(url.startsWith("http") || url.startsWith("https"))) {
        //非http https协议 不动作
        return true;
    }

    //http https协议 在本webView中加载

    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if(VideoFormatUtil.containsVideoExtension(extension)){
        detectedTaskUrlQueue.add(new DetectedVideoInfo(url,currentUrl,currentTitle));
        Log.d("MainActivity", "shouldOverrideUrlLoading detectTaskUrlList.add(url):" + url);
        return true;
    }

    Log.d("MainActivity", "shouldOverrideUrlLoading url="+url);
    view.loadUrl(url);
    return true;
}
 
Example 10
Source Project: wildfly-samples   Source File: CordovaResourceApi.java    License: MIT License 6 votes vote down vote up
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);
}
 
Example 11
Source Project: qiniu-lab-android   Source File: LocalStorageProvider.java    License: MIT License 6 votes vote down vote up
@Override
public String getDocumentType(final String documentId) throws FileNotFoundException {
    File file = new File(documentId);
    if (file.isDirectory())
        return Document.MIME_TYPE_DIR;
    // From FileProvider.getType(Uri)
    final int lastDot = file.getName().lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = file.getName().substring(lastDot + 1);
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example 12
Source Project: Android-RTEditor   Source File: MediaUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static File createUniqueFile(File targetFolder, String originalFile, String mimeType, boolean keepOriginal) {
    /*
     * We try to get the extension from the file name first.
     * If that fails (e.g. for images provided by the picasa content provider)
     * we use the mime type to determine the extension.
     * The extension is important to be able to determine the correct content type
     * once we create a MIME message.
     */
    String extension = FilenameUtils.getExtension(originalFile);
    if (isNullOrEmpty(extension)) {
        extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
    }

    String random = Long.toString(Math.round(Math.random() * Integer.MAX_VALUE));        // random part
    long timestamp = Calendar.getInstance().getTimeInMillis();                            // time stamp

    if (keepOriginal) {
        String baseName = FilenameUtils.getBaseName(originalFile);
        return new File(targetFolder + File.separator + baseName + "_" + random + "_" + timestamp + "." + extension);
    } else {
        return new File(targetFolder + File.separator + random + "_" + timestamp + "." + extension);
    }
}
 
Example 13
Source Project: countly-sdk-cordova   Source File: CordovaResourceApi.java    License: MIT License 6 votes vote down vote up
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);
}
 
Example 14
Source Project: FireFiles   Source File: DocumentArchive.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 15
Source Project: react-native-3d-model-view   Source File: Object3DBuilder.java    License: MIT License 5 votes vote down vote up
public static void loadV6AsyncParallel(Context context, final URL url,
									   final Callback callback) {

	final String modelId = url.toString().toLowerCase();
	String fileExtension = MimeTypeMap.getFileExtensionFromUrl(modelId);
	if (fileExtension.equals("obj")) {
		WavefrontLoader2.loadAsync(context, url, callback);
	} else if (fileExtension.equals("dae")) {
		ColladaLoader.loadAsync(url, callback);
	}
}
 
Example 16
public static boolean isMarkdown(@Nullable String name) {
    if (InputHelper.isEmpty(name)) return false;
    name = name.toLowerCase();
    for (String value : MARKDOWN_EXTENSIONS) {
        String extension = MimeTypeMap.getFileExtensionFromUrl(name);
        if ((extension != null && value.replace(".", "").equals(extension)) ||
                name.equalsIgnoreCase("README") || name.endsWith(value))
            return true;
    }
    return false;
}
 
Example 17
Source Project: deltachat-android   Source File: MediaUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static String getExtensionFromMimeType(String contentType) {
  String extension =  MimeTypeMap.getSingleton().getExtensionFromMimeType(contentType);
  if (extension != null) {
    return extension;
  }

  //custom handling needed for unsupported extensions on Android 4.X
  switch (contentType) {
    case AUDIO_AAC:
      return "aac";
    case IMAGE_WEBP:
      return "webp";
  }
  return null;
}
 
Example 18
Source Project: FireFiles   Source File: MimeTypes.java    License: Apache License 2.0 5 votes vote down vote up
public static String getMimeTypeFromExtension(String extension) {
    String type = null;

    if (!TextUtils.isEmpty(extension)) {
        final String extensionLowerCase = extension.toLowerCase(Locale.getDefault());
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extensionLowerCase);
        if (type == null) {
            //extra mime types map
            type = MIME_TYPES.get(extensionLowerCase);
        }
    }
    return type;
}
 
Example 19
Source Project: twitter-kit-android   Source File: FileUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @return The MIME type for the given file.
 */
static String getMimeType(File file) {
    final String ext = getExtension(file.getName());
    if (!TextUtils.isEmpty(ext)) {
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext);
    }
    // default from https://dev.twitter.com/rest/public/uploading-media
    return "application/octet-stream";
}
 
Example 20
Source Project: Android   Source File: UpdataService.java    License: MIT License 5 votes vote down vote up
public String getMIMEType(File var0) {
    String var1 = "";
    String var2 = var0.getName();
    String var3 = var2.substring(var2.lastIndexOf(".") + 1, var2.length()).toLowerCase();
    var1 = MimeTypeMap.getSingleton().getMimeTypeFromExtension(var3);
    return var1;
}
 
Example 21
Source Project: Hentoid   Source File: FileHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getExtensionFromMimeType(@NonNull String mimeType) {
    if (mimeType.isEmpty()) return null;

    String result = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
    // Exceptions that MimeTypeMap does not support
    if (null == result) {
        if (mimeType.equals("image/apng") || mimeType.equals("image/vnd.mozilla.apng"))
            return "png";
    }
    return result;
}
 
Example 22
Source Project: Clip-Stack   Source File: BackupObject.java    License: MIT License 5 votes vote down vote up
public void openInEditor() {
    context.startActivity(new Intent()
                    .setAction(android.content.Intent.ACTION_VIEW)
                    .setDataAndType(Uri.fromFile(backupFile), MimeTypeMap.getSingleton().getMimeTypeFromExtension("txt"))
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    );
}
 
Example 23
Source Project: edx-app-android   Source File: UserAPI.java    License: Apache License 2.0 5 votes vote down vote up
public Call<ResponseBody> setProfileImage(@NonNull String username, @NonNull final File file) {
    final String mimeType = "image/jpeg";
    return userService.setProfileImage(
            username,
            "attachment;filename=filename." + MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType),
            RequestBody.create(MediaType.parse(mimeType), file));
}
 
Example 24
Source Project: letv   Source File: FileProvider.java    License: Apache License 2.0 5 votes vote down vote up
public String getType(Uri uri) {
    File file = this.mStrategy.getFileForUri(uri);
    int lastDot = file.getName().lastIndexOf(46);
    if (lastDot >= 0) {
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(file.getName().substring(lastDot + 1));
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}
 
Example 25
Source Project: FireFiles   Source File: HeatMapProvider.java    License: Apache License 2.0 5 votes vote down vote up
private static String getTypeForName(String name) {
    final int lastDot = name.lastIndexOf('.');
    if (lastDot >= 0) {
        final String extension = name.substring(lastDot + 1).toLowerCase();
        final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
        if (mime != null) {
            return mime;
        }
    }

    return BASIC_MIME_TYPE;
}
 
Example 26
Source Project: PLDroidShortVideo   Source File: MultipleComposeActivity.java    License: Apache License 2.0 5 votes vote down vote up
private PLComposeItem createImageItem(String path) {
    Log.i(TAG, "createImageItem path : " + path);
    PLComposeItem item;
    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(path);
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
    Log.i(TAG, "createImageItem fileExtension : " + fileExtension);
    Log.i(TAG, "createImageItem mimeType : " + mimeType);

    if (mimeType != null && mimeType.contains("gif")) {
        item = createGIFImageItem(path);
    } else {
        item = createNormalImageItem(path);
    }
    return item;
}
 
Example 27
Source Project: Silence   Source File: MediaUtil.java    License: GNU General Public License v3.0 5 votes vote down vote up
public static @Nullable String getMimeType(Context context, Uri uri) {
  if (uri == null) return null;

  if (PersistentBlobProvider.isAuthority(context, uri)) {
    return PersistentBlobProvider.getMimeType(context, uri);
  }

  String type = context.getContentResolver().getType(uri);
  if (type == null) {
    final String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
    type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension.toLowerCase());
  }
  return getCorrectedMimeType(type);
}
 
Example 28
Source Project: Kandroid   Source File: TaskDetailActivity.java    License: GNU General Public License v3.0 5 votes vote down vote up
@Override
        public void onDownloadTaskFile(boolean success, int id, String data) {
            if (success) {
                byte[] inData = Base64.decode(data, Base64.DEFAULT);
                for (KanboardTaskFile f: files) {
                    if (f.getId() == id) {
                        try {
                            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), f.getName());
                            FileOutputStream outData = new FileOutputStream(file);
                            outData.write(inData);
                            outData.close();
                            String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()));
                            if (mime == null) {
                                mime = "application/octet-stream";
                            }
                            if (BuildConfig.DEBUG) {
                                Log.d(Constants.TAG, Uri.fromFile(file).toString());
                                Log.d(Constants.TAG, mime);
                            }
                            DownloadManager dm = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                            dm.addCompletedDownload(file.getName(), "Kandroid download", false, mime, file.getPath(), file.length(), true);
//                            Snackbar.make(findViewById(R.id.root_layout), String.format(Locale.getDefault(), "Saved file to: %s", file.getPath()), Snackbar.LENGTH_LONG).show();
                        } catch (IOException e) {
                            Log.w(Constants.TAG, "IOError writing file");
                            e.printStackTrace();
                        }
                        break;
                    }
                }
            } else {
                Snackbar.make(findViewById(R.id.root_layout), "Unable to download file", Snackbar.LENGTH_LONG).show();
            }
        }
 
Example 29
Source Project: SimplicityBrowser   Source File: Helpers.java    License: MIT License 5 votes vote down vote up
public static String getMimeType(String url) {
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return null;
}
 
Example 30
Source Project: letv   Source File: RawDocumentFile.java    License: Apache License 2.0 5 votes vote down vote up
private static String getTypeForName(String name) {
    int lastDot = name.lastIndexOf(46);
    if (lastDot >= 0) {
        String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(name.substring(lastDot + 1).toLowerCase());
        if (mime != null) {
            return mime;
        }
    }
    return "application/octet-stream";
}