Java Code Examples for android.webkit.MimeTypeMap#getSingleton()

The following examples show how to use android.webkit.MimeTypeMap#getSingleton() . 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 File: FormUploadUtil.java    From commcare-android with Apache License 2.0 6 votes vote down vote up
/**
 * Use the file's extension to determine if it has an audio,
 * video, or image mimetype.
 *
 * @return true if the file has an audio, image, or video mimetype
 */
private static boolean isAudioVisualMimeType(String filename) {
    MimeTypeMap mtm = MimeTypeMap.getSingleton();
    String[] filenameSegments = filename.split("\\.");
    if (filenameSegments.length > 1) {
        // use the file extension to determine the mimetype
        String ext = filenameSegments[filenameSegments.length - 1];
        String mimeType = mtm.getMimeTypeFromExtension(ext);

        return (mimeType != null) &&
                (mimeType.startsWith("audio") ||
                        mimeType.startsWith("image") ||
                        mimeType.startsWith("video"));
    }

    return false;
}
 
Example 2
Source File: BUtility.java    From appcan-android with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void internalInstallApk(Context context, String inAppPath){
    // install apk.
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    MimeTypeMap type = MimeTypeMap.getSingleton();
    String mime = type.getMimeTypeFromExtension("apk");
    Uri apkFileUri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        apkFileUri = BUtility.getUriForFileWithFileProvider(context, inAppPath);
    }else {
        apkFileUri = Uri.parse("file://" + inAppPath);
    }
    intent.setDataAndType(apkFileUri, mime);
    context.startActivity(intent);
}
 
Example 3
Source File: RootUtils.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 4
Source File: MimeTypes.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
public static String getMimeType(String path) {
    String type = ALL_MIME_TYPES;
    final String extension = getExtension(path);

    // mapping extension to system mime types
    if (extension != null && !extension.isEmpty()) {
        final String extensionLowerCase = extension.toLowerCase();
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extensionLowerCase);
        if (type == null) {
            type = MIME_TYPES.get(extensionLowerCase);
        }
    }
    if (type == null) type = ALL_MIME_TYPES;
    return type;
}
 
Example 5
Source File: MimeTypes.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Get Mime Type of a file
 * @param file the file of which mime type to get
 * @return Mime type in form of String
 */
public static String getMimeType(File file) {
    if (file.isDirectory()) {
        return null;
    }

    String type = ALL_MIME_TYPES;
    final String extension = getExtension(file.getName());

    // mapping extension to system mime types
    if (extension != null && !extension.isEmpty()) {
        final String extensionLowerCase = extension.toLowerCase();
        final MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extensionLowerCase);
        if (type == null) {
            type = MIME_TYPES.get(extensionLowerCase);
        }
    }
    if (type == null) type = ALL_MIME_TYPES;
    return type;
}
 
Example 6
Source File: FilesActivity.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
private void viewFileInExternalApp(File result) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String ext = result.getName().substring(result.getName().indexOf(".") + 1);
    String type = mime.getMimeTypeFromExtension(ext);

    intent.setDataAndType(Uri.fromFile(result), type);

    // Check for a handler first to avoid a crash
    PackageManager manager = getPackageManager();
    List<ResolveInfo> resolveInfo = manager.queryIntentActivities(intent, 0);
    if (resolveInfo.size() > 0) {
        startActivity(intent);
    }
}
 
Example 7
Source File: FileChooserDialog.java    From APlayer with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 8
Source File: ShareUtils.java    From Gallery-example with GNU General Public License v3.0 5 votes vote down vote up
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();
    }
}
 
Example 9
Source File: ActionManager.java    From droid-stealth with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Gets the intent to share a single IndexedFile with
 *
 * @param with the file to share
 * @return share intent
 */
private Intent getShareIntentSingle(IndexedFile with) {

	Intent intent = new Intent();
	intent.setAction(Intent.ACTION_SEND);

	MimeTypeMap map = MimeTypeMap.getSingleton();
	String mimeType = map.getMimeTypeFromExtension(with.getExtension().substring(1));
	intent.setType(mimeType);

	Uri uri = Uri.fromFile(with.getUnlockedFile());
	intent.putExtra(Intent.EXTRA_STREAM, uri);

	return intent;
}
 
Example 10
Source File: FileHelper.java    From OmniList with GNU Affero General Public License v3.0 5 votes vote down vote up
private static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}
 
Example 11
Source File: HelperGetDataFromOtherApp.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
public static FileType getMimeType(Uri uri) {
    String extension;
    FileType fileType = FileType.file;

    //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(G.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());
    }

    if (extension == null) {
        return null;
    }
    extension = extension.toLowerCase();

    if (extension.endsWith("jpg") || extension.endsWith("jpeg") || extension.endsWith("png") || extension.endsWith("bmp") || extension.endsWith(".tiff")) {
        fileType = FileType.image;
    } else if (extension.endsWith("mp3") || extension.endsWith("ogg") || extension.endsWith("wma") || extension.endsWith("m4a") || extension.endsWith("amr") || extension.endsWith("wav") || extension.endsWith(".mid") || extension.endsWith(".midi")) {
        fileType = FileType.audio;
    } else if (extension.endsWith("mp4") || extension.endsWith("3gp") || extension.endsWith("avi") || extension.endsWith("mpg") || extension.endsWith("flv") || extension.endsWith("wmv") || extension.endsWith("m4v") || extension.endsWith(".mpeg")) {
        fileType = FileType.video;
    }

    return fileType;
}
 
Example 12
Source File: FileUtils.java    From AndroidPicker with MIT License 5 votes vote down vote up
/**
 * 获取文件的MIME类型
 */
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.verbose(pathOrUrl + ": " + mimeType);
    return mimeType;
}
 
Example 13
Source File: FileUtils.java    From Applozic-Android-SDK with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);

    if (TextUtils.isEmpty(extension) && url.contains(".")) {
        extension = url.substring(url.lastIndexOf('.') + 1).toLowerCase();
    }

    if (!TextUtils.isEmpty(extension)) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension.toLowerCase());
    }

    return type;
}
 
Example 14
Source File: IntentUriGenerator.java    From TvAppRepo with Apache License 2.0 5 votes vote down vote up
public static String generateVideoPlayback(File myFile) {
    MimeTypeMap myMime = MimeTypeMap.getSingleton();
    Intent newIntent = new Intent(Intent.ACTION_VIEW);
    String mimeType = myMime.getMimeTypeFromExtension(fileExt(myFile.getAbsolutePath()).substring(1));
    newIntent.setDataAndType(Uri.fromFile(myFile),mimeType);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    return newIntent.toUri(Intent.URI_INTENT_SCHEME);
}
 
Example 15
Source File: ConsoleContentProvider.java    From matrix-android-console with Apache License 2.0 5 votes vote down vote up
@Override
public String getType(Uri arg0) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(arg0.toString());
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }

    return type;
}
 
Example 16
Source File: ProxyCacheUtils.java    From VideoOS-Android-SDK with GNU General Public License v3.0 4 votes vote down vote up
static String getSupposablyMime(String url) {
    MimeTypeMap mimes = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    return TextUtils.isEmpty(extension) ? null : mimes.getMimeTypeFromExtension(extension);
}
 
Example 17
Source File: ProxyCacheUtils.java    From GSYVideoPlayer with Apache License 2.0 4 votes vote down vote up
static String getSupposablyMime(String url) {
    MimeTypeMap mimes = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    return TextUtils.isEmpty(extension) ? null : mimes.getMimeTypeFromExtension(extension);
}
 
Example 18
Source File: IOCipherContentProvider.java    From CameraV with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onCreate() {
	mimeTypeMap = MimeTypeMap.getSingleton();
	return true;
}
 
Example 19
Source File: TagSettings.java    From com.ruuvi.station with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private String getMimeType(Uri uri) {
    ContentResolver cR = getApplicationContext().getContentResolver();
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    return mime.getExtensionFromMimeType(cR.getType(uri));
}
 
Example 20
Source File: AndroidUtilities.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public static void openForView(TLObject media, Activity activity) {
    if (media == null || activity == null) {
        return;
    }
    String fileName = FileLoader.getAttachFileName(media);
    File f = FileLoader.getPathToAttach(media, true);
    if (f != null && f.exists()) {
        String realMimeType = null;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        int idx = fileName.lastIndexOf('.');
        if (idx != -1) {
            String ext = fileName.substring(idx + 1);
            realMimeType = myMime.getMimeTypeFromExtension(ext.toLowerCase());
            if (realMimeType == null) {
                if (media instanceof TLRPC.TL_document) {
                    realMimeType = ((TLRPC.TL_document) media).mime_type;
                }
                if (realMimeType == null || realMimeType.length() == 0) {
                    realMimeType = null;
                }
            }
        }
        if (Build.VERSION.SDK_INT >= 24) {
            intent.setDataAndType(FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f), realMimeType != null ? realMimeType : "text/plain");
        } else {
            intent.setDataAndType(Uri.fromFile(f), realMimeType != null ? realMimeType : "text/plain");
        }
        if (realMimeType != null) {
            try {
                activity.startActivityForResult(intent, 500);
            } catch (Exception e) {
                if (Build.VERSION.SDK_INT >= 24) {
                    intent.setDataAndType(FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", f), "text/plain");
                } else {
                    intent.setDataAndType(Uri.fromFile(f), "text/plain");
                }
                activity.startActivityForResult(intent, 500);
            }
        } else {
            activity.startActivityForResult(intent, 500);
        }
    }
}