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

The following examples show how to use android.webkit.MimeTypeMap#getMimeTypeFromExtension() . 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: MimeTypes.java    From SimpleExplorer with GNU General Public License v3.0 6 votes vote down vote up
public static String getMimeType(File file) {
    if (file.isDirectory()) {
        return null;
    }

    String type = null;
    final String extension = SimpleUtils.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;
}
 
Example 2
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 3
Source File: EUtil.java    From appcan-android with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void installApp(Context context, String inAppPath) {
    if (null == inAppPath || 0 == inAppPath.trim().length()) {
        return;
    }
    String reallyPath = "";
    File file = new File(inAppPath);
    if (file.exists()) {
        reallyPath = inAppPath;
    } else {
        reallyPath = copyFileToStorage(context, inAppPath);
        if (null == reallyPath) {
            return;
        }
    }
    // 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");
    reallyPath = reallyPath.contains("file://") ? reallyPath : ("file://" + reallyPath);
    intent.setDataAndType(Uri.parse(reallyPath), mime);
    context.startActivity(intent);
}
 
Example 4
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 5
Source File: DownloadReceiver.java    From 4pdaClient-plus with Apache License 2.0 5 votes vote down vote up
private Intent getRunFileIntent(String filePath) {
    MimeTypeMap myMime = MimeTypeMap.getSingleton();
    Intent newIntent = new Intent(Intent.ACTION_VIEW);
    String mimeType = myMime.getMimeTypeFromExtension(FileUtils.fileExt(filePath).substring(1));
    newIntent.setDataAndType(Uri.parse("file://" + filePath), mimeType);
    newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    return newIntent;
}
 
Example 6
Source File: ContentViewActivity.java    From PKUCourses with GNU General Public License v3.0 5 votes vote down vote up
public Drawable getIcon(String fileName) {
    final Intent innt = new Intent(Intent.ACTION_VIEW);
    MimeTypeMap myMime = MimeTypeMap.getSingleton();
    String mimeType = myMime.getMimeTypeFromExtension(fileExt(fileName));
    innt.setType(mimeType);
    final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(innt, 0);
    if (matches.size() == 0)
        return null;
    return matches.get(0).loadIcon(getPackageManager());
}
 
Example 7
Source File: FileUtil.java    From Orin with GNU General Public License v3.0 5 votes vote down vote up
public static boolean fileIsMimeType(File file, String mimeType, MimeTypeMap mimeTypeMap) {
    if (mimeType == null || mimeType.equals("*/*")) {
        return true;
    } else {
        // get the file mime type
        String filename = file.toURI().toString();
        int dotPos = filename.lastIndexOf('.');
        if (dotPos == -1) {
            return false;
        }
        String fileExtension = filename.substring(dotPos + 1).toLowerCase();
        String fileType = mimeTypeMap.getMimeTypeFromExtension(fileExtension);
        if (fileType == null) {
            return false;
        }
        // check the 'type/subtype' pattern
        if (fileType.equals(mimeType)) {
            return true;
        }
        // check the 'type/*' pattern
        int mimeTypeDelimiter = mimeType.lastIndexOf('/');
        if (mimeTypeDelimiter == -1) {
            return false;
        }
        String mimeTypeMainType = mimeType.substring(0, mimeTypeDelimiter);
        String mimeTypeSubtype = mimeType.substring(mimeTypeDelimiter + 1);
        if (!mimeTypeSubtype.equals("*")) {
            return false;
        }
        int fileTypeDelimiter = fileType.lastIndexOf('/');
        if (fileTypeDelimiter == -1) {
            return false;
        }
        String fileTypeMainType = fileType.substring(0, fileTypeDelimiter);
        if (fileTypeMainType.equals(mimeTypeMainType)) {
            return true;
        }
    }
    return false;
}
 
Example 8
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 9
Source File: Utils.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
public 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 10
Source File: Utils.java    From android_tv_metro with Apache License 2.0 5 votes vote down vote up
public 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: 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 12
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 13
Source File: ProjectManagerActivity.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: FileUtil.java    From VinylMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static boolean fileIsMimeType(File file, String mimeType, MimeTypeMap mimeTypeMap) {
    if (mimeType == null || mimeType.equals("*/*")) {
        return true;
    } else {
        // get the file mime type
        String filename = file.toURI().toString();
        int dotPos = filename.lastIndexOf('.');
        if (dotPos == -1) {
            return false;
        }
        String fileExtension = filename.substring(dotPos + 1).toLowerCase();
        String fileType = mimeTypeMap.getMimeTypeFromExtension(fileExtension);
        if (fileType == null) {
            return false;
        }
        // check the 'type/subtype' pattern
        if (fileType.equals(mimeType)) {
            return true;
        }
        // check the 'type/*' pattern
        int mimeTypeDelimiter = mimeType.lastIndexOf('/');
        if (mimeTypeDelimiter == -1) {
            return false;
        }
        String mimeTypeMainType = mimeType.substring(0, mimeTypeDelimiter);
        String mimeTypeSubtype = mimeType.substring(mimeTypeDelimiter + 1);
        if (!mimeTypeSubtype.equals("*")) {
            return false;
        }
        int fileTypeDelimiter = fileType.lastIndexOf('/');
        if (fileTypeDelimiter == -1) {
            return false;
        }
        String fileTypeMainType = fileType.substring(0, fileTypeDelimiter);
        return fileTypeMainType.equals(mimeTypeMainType);
    }
}
 
Example 15
Source File: FilesAdapter.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
public void bind(Metadata item) {
    mItem = item;
    mTextView.setText(mItem.getName());

    // Load based on file path
    // Prepending a magic scheme to get it to
    // be picked up by DropboxPicassoRequestHandler

    if (item instanceof FileMetadata) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        String ext = item.getName().substring(item.getName().indexOf(".") + 1);
        String type = mime.getMimeTypeFromExtension(ext);
        if (type != null && type.startsWith("image/")) {
            mPicasso.load(FileThumbnailRequestHandler.buildPicassoUri((FileMetadata)item))
                    .placeholder(R.drawable.ic_photo_grey_600_36dp)
                    .error(R.drawable.ic_photo_grey_600_36dp)
                    .into(mImageView);
        } else {
            mPicasso.load(R.drawable.ic_insert_drive_file_blue_36dp)
                    .noFade()
                    .into(mImageView);
        }
    } else if (item instanceof FolderMetadata) {
        mPicasso.load(R.drawable.ic_folder_blue_36dp)
                .noFade()
                .into(mImageView);
    }
}
 
Example 16
Source File: AndroidUtilities.java    From Telegram 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);
        }
    }
}
 
Example 17
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 18
Source File: MoodleCourseDetailsFragment.java    From ETSMobile-Android2 with Apache License 2.0 4 votes vote down vote up
@Override
public void onActivityCreated(Bundle onSavedInstanceState) {
    super.onActivityCreated(onSavedInstanceState);

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(enqueue);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {

                        String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        MimeTypeMap map = MimeTypeMap.getSingleton();
                        String ext = MimeTypeMap.getFileExtensionFromUrl(uriString);
                        String type = map.getMimeTypeFromExtension(ext);
                        Uri uri = Uri.parse(uriString);

                        if (type == null)
                            type = "*/*";

                        Intent openFile = new Intent(Intent.ACTION_VIEW);
                        openFile.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            File file = new File(uri.getPath());
                            Uri fileProviderUri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
                            openFile.setDataAndType(fileProviderUri, type);
                        } else {
                            openFile.setDataAndType(uri, type);
                        }
                        try {
                            startActivity(openFile);
                        } catch (ActivityNotFoundException e) {
                            Toast.makeText(getActivity(), getString(R.string.cannot_open_file), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }
        }
    };
    getActivity().registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

    queryMoodleCoreCourses(moodleCourseId);

}
 
Example 19
Source File: AndroidUtilities.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
public static void openForView(TLObject media, Activity activity) throws Exception
{
    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);
        }
    }
}
 
Example 20
Source File: CustomFileChooser.java    From ROMInstaller with MIT License 2 votes vote down vote up
boolean fileIsMimeType(File file, String mimeType, MimeTypeMap mimeTypeMap) {

        if (mimeType == null || mimeType.equals("*/*")) {

            return true;

        } else {

            String filename = file.toURI().toString();

            int dotPos = filename.lastIndexOf('.');

            if (dotPos == -1) {

                return false;

            }

            String fileExtension = filename.substring(dotPos + 1);

            String fileType = mimeTypeMap.getMimeTypeFromExtension(fileExtension);

            if (fileType == null) {

                return false;

            }

            if (fileType.equals(mimeType)) {

                return true;

            }

            int mimeTypeDelimiter = mimeType.lastIndexOf('/');

            if (mimeTypeDelimiter == -1) {

                return false;

            }

            String mimeTypeMainType = mimeType.substring(0, mimeTypeDelimiter);

            String mimeTypeSubtype = mimeType.substring(mimeTypeDelimiter + 1);

            if (!mimeTypeSubtype.equals("*")) {

                return false;

            }

            int fileTypeDelimiter = fileType.lastIndexOf('/');

            if (fileTypeDelimiter == -1) {

                return false;

            }

            String fileTypeMainType = fileType.substring(0, fileTypeDelimiter);

            if (fileTypeMainType.equals(mimeTypeMainType)) {

                return true;

            }

        }

        return false;

    }