Example usage for android.webkit MimeTypeMap getSingleton

List of usage examples for android.webkit MimeTypeMap getSingleton

Introduction

In this page you can find the example usage for android.webkit MimeTypeMap getSingleton.

Prototype

public static MimeTypeMap getSingleton() 

Source Link

Document

Get the singleton instance of MimeTypeMap.

Usage

From source file:com.commonsware.android.diceware.PassphraseFragment.java

private static Uri makeLocalCopy(Context ctxt, ContentResolver resolver, Uri document) throws IOException {
    DocumentFileCompat docFile = buildDocFileForUri(ctxt, document);
    Uri result = null;/*from ww  w.j  a v  a  2s.co  m*/

    if (docFile.getName() != null) {
        String ext = MimeTypeMap.getSingleton().getExtensionFromMimeType(docFile.getType());

        if (ext != null) {
            ext = "." + ext;
        }

        File f = File.createTempFile("cw_", ext, ctxt.getFilesDir());

        docFile.copyTo(f);
        result = Uri.fromFile(f);
    }

    return (result);
}

From source file:de.petermoesenthin.alarming.util.FileUtil.java

/**
 * Returns a mime type for a file in given path.
 *
 * @param path Path to the file.//from  www .j a  va2  s  .  co m
 * @return Null if no MIME type is found.
 */
public static String getMimeType(Context context, String path) {
    Log.d(DEBUG_TAG, "Checking mime type for " + path);
    String mimeType = null;
    String extension = FilenameUtils.getExtension(path);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        mimeType = mime.getMimeTypeFromExtension(extension);
        Log.d(DEBUG_TAG, "MimeTypeMap found " + mimeType + " for extension " + extension);
    }
    if (mimeType == null) {
        ContentResolver contentResolver = context.getContentResolver();
        mimeType = contentResolver.getType(Uri.parse(path));
        Log.d(DEBUG_TAG, "ContentResolver found " + mimeType + ".");
    }
    return mimeType;
}

From source file:com.cerema.cloud2.files.InstantUploadBroadcastReceiver.java

private void handleConnectivityAction(Context context, Intent intent) {
    if (!instantPictureUploadEnabled(context)) {
        Log_OC.d(TAG, "Instant upload disabled, don't upload anything");
        return;/*from   w w  w .  j ava2s .co m*/
    }

    if (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context)) {
        Account account = AccountUtils.getCurrentOwnCloudAccount(context);
        if (account == null) {
            Log_OC.w(TAG, "No account found for instant upload, aborting");
            return;
        }

        Intent i = new Intent(context, FileUploader.class);
        i.putExtra(FileUploader.KEY_ACCOUNT, account);
        i.putExtra(FileUploader.KEY_CANCEL_ALL, true);
        context.startService(i);
    }

    if (!intent.hasExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY) && isOnline(context)
            && (!instantPictureUploadViaWiFiOnly(context)
                    || (instantPictureUploadViaWiFiOnly(context) == isConnectedViaWiFi(context) == true))) {
        DbHandler db = new DbHandler(context);
        Cursor c = db.getAwaitingFiles();
        if (c.moveToFirst()) {
            do {
                if (instantPictureUploadViaWiFiOnly(context) && !isConnectedViaWiFi(context)) {
                    break;
                }

                String account_name = c.getString(c.getColumnIndex("account"));
                String file_path = c.getString(c.getColumnIndex("path"));
                File f = new File(file_path);
                if (f.exists()) {
                    Account account = new Account(account_name, MainApp.getAccountType());

                    String mimeType = null;
                    try {
                        mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                                f.getName().substring(f.getName().lastIndexOf('.') + 1));

                    } catch (Throwable e) {
                        Log_OC.e(TAG,
                                "Trying to find out MIME type of a file without extension: " + f.getName());
                    }
                    if (mimeType == null)
                        mimeType = "application/octet-stream";

                    Intent i = new Intent(context, FileUploader.class);
                    i.putExtra(FileUploader.KEY_ACCOUNT, account);
                    i.putExtra(FileUploader.KEY_LOCAL_FILE, file_path);
                    i.putExtra(FileUploader.KEY_REMOTE_FILE,
                            FileStorageUtils.getInstantUploadFilePath(context, f.getName()));
                    i.putExtra(FileUploader.KEY_UPLOAD_TYPE, FileUploader.UPLOAD_SINGLE_FILE);
                    i.putExtra(FileUploader.KEY_INSTANT_UPLOAD, true);

                    // instant upload behaviour
                    i = addInstantUploadBehaviour(i, context);

                    context.startService(i);

                } else {
                    Log_OC.w(TAG, "Instant upload file " + f.getAbsolutePath() + " dont exist anymore");
                }
            } while (c.moveToNext());
        }
        c.close();
        db.close();
    }

}

From source file:com.owncloud.android.utils.MimeTypeUtil.java

/**
 * Extracts the mime type for the given file.
 *
 * @param file the file to be analyzed/*  w ww.java 2  s.co  m*/
 * @return the file's mime type
 */
private static String extractMimeType(File file) {
    Uri selectedUri = Uri.fromFile(file);
    String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString().toLowerCase(Locale.ROOT));
    return MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
}

From source file:net.sf.xfd.provider.ProviderBase.java

public String getTypeFast(@CanonPath String path, String name, Stat stat) throws FileNotFoundException {
    if ("/".equals(path)) {
        return MIME_TYPE_DIR;
    }//www  .  jav  a 2s  .  c o  m

    final OS os = getOS();
    if (os == null) {
        return DEFAULT_MIME;
    }

    try {
        @Fd
        int fd = Fd.NIL;

        @DirFd
        int parentFd = os.opendir(extractParent(path));
        try {
            int flags = 0;

            if (!os.faccessat(parentFd, name, OS.F_OK)) {
                flags = OS.AT_SYMLINK_NOFOLLOW;
            }

            os.fstatat(parentFd, name, stat, flags);

            if (stat.type == null) {
                return DEFAULT_MIME;
            }

            switch (stat.type) {
            case LINK:
                return LINK_MIME;
            case DIRECTORY:
                return MIME_TYPE_DIR;
            case CHAR_DEV:
                return CHAR_DEV_MIME;
            default:
            }

            final String extension = getExtensionFast(name);

            final MimeTypeMap mimeMap = MimeTypeMap.getSingleton();

            final String foundMime = mimeMap.getMimeTypeFromExtension(extension);
            if (foundMime != null) {
                return foundMime;
            }

            final boolean canSniffContent = stat.type == FsType.FILE
                    && (stat.st_size != 0 || isPossiblySpecial(stat));

            if (flags == 0) {
                os.fstatat(parentFd, name, stat, OS.AT_SYMLINK_NOFOLLOW);

                if (stat.type == FsType.LINK) {
                    CharSequence resolved = null;

                    if (canSniffContent) {
                        try {
                            fd = os.openat(parentFd, name, OS.O_RDONLY, 0);

                            resolved = os.readlinkat(DirFd.NIL, fdPath(fd));
                        } catch (IOException ioe) {
                            LogUtil.logCautiously("Unable to open target of " + name, ioe);
                        }
                    }

                    if (resolved == null) {
                        resolved = os.readlinkat(parentFd, name);

                        if (resolved.charAt(0) == '/') {
                            resolved = canonString(resolved);
                        }
                    }

                    final String linkTargetExtension = getExtensionFromPath(resolved);

                    if (linkTargetExtension != null && !linkTargetExtension.equals(extension)) {
                        final String sortaFastMime = mimeMap.getMimeTypeFromExtension(linkTargetExtension);
                        if (sortaFastMime != null) {
                            return sortaFastMime;
                        }
                    }
                }
            }

            if (canSniffContent) {
                if (fd < 0) {
                    fd = os.openat(parentFd, name, OS.O_RDONLY, 0);
                }

                final String contentInfo = magic.guessMime(fd);

                if (contentInfo != null) {
                    return contentInfo;
                }
            }

            switch (stat.type) {
            case LINK:
                return LINK_MIME;
            case DOMAIN_SOCKET:
                return SOCK_MIME;
            case NAMED_PIPE:
                return FIFO_MIME;
            case FILE:
                if (stat.st_size == 0) {
                    return EMPTY_MIME;
                }
            default:
                return DEFAULT_MIME;
            }
        } finally {
            if (fd > 0) {
                os.dispose(fd);
            }

            os.dispose(parentFd);
        }
    } catch (IOException e) {
        LogUtil.logCautiously("Encountered IO error during mime sniffing", e);

        throw new FileNotFoundException("Failed to stat " + name);
    }
}

From source file:com.polyvi.xface.util.XFileUtils.java

/**
 * ?urlMIMEType/*from  w  w  w.ja  v a 2  s . com*/
 *
 * @paramm url url?
 * @return MIMEType
 */
public static String getMIMEType(String url) {
    int dotIndex = url.lastIndexOf(".");
    /* ???? */
    return dotIndex < 0 ? "*/*"
            : MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(url.substring(dotIndex + 1, url.length()).toLowerCase());
}

From source file:com.waz.zclient.pages.main.conversation.views.row.message.views.FileMessageViewController.java

private String getFileExtension(Asset asset) {
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    return mimeTypeMap.getExtensionFromMimeType(asset.getMimeType());
}

From source file:io.nuclei.cyto.share.PackageTargetManager.java

/**
 * Set default intent data/*from   ww w  .j a v  a2  s  .  c  o m*/
 */
protected void onSetDefault(Context context, String packageName, String authority, Intent intent, String text) {
    intent.putExtra(Intent.EXTRA_TEXT, text);
    if (!TextUtils.isEmpty(mSubject))
        intent.putExtra(Intent.EXTRA_SUBJECT, mSubject);
    if (mUri != null || mFile != null) {
        Uri uri = mUri;
        String type = "*/*";
        if (mFile != null) {
            uri = FileProvider.getUriForFile(context, authority, mFile);
            final int lastDot = mFile.getName().lastIndexOf('.');
            if (lastDot >= 0) {
                String extension = mFile.getName().substring(lastDot + 1);
                String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
                if (mimeType != null)
                    type = mimeType;
            }
        }
        intent.setDataAndType(intent.getData(), type);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        if (packageName != null) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    } else {
        intent.setType("text/plain");
    }
    if (!TextUtils.isEmpty(mEmail)) {
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { mEmail });
        intent.setData(Uri.parse("mailto:"));
    }
}

From source file:eu.codeplumbers.cosi.activities.CozyActivity.java

@Override
public void onListFragmentInteraction(File file) {
    if (currentFragment instanceof FileManagerFragment) {
        if (!file.isFile()) {
            String path = file.getPath().equals("") ? "/" : file.getPath() + "/";
            ((FileManagerFragment) currentFragment).setCurrentPath(path + file.getName());
            ((FileManagerFragment) currentFragment).refreshList();
        } else {/* w  ww  . java 2  s  . c o m*/
            if (file.getDownloaded()) {
                MimeTypeMap myMime = MimeTypeMap.getSingleton();
                Intent newIntent = new Intent(Intent.ACTION_VIEW);
                String mimeType = myMime
                        .getMimeTypeFromExtension(FileUtils.fileExt(file.getName()).substring(1));
                newIntent.setDataAndType(Uri.fromFile(file.getLocalPath()), mimeType);
                newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                try {
                    startActivity(newIntent);
                } catch (ActivityNotFoundException e) {
                    Toast.makeText(this, "No handler for this type of file.", Toast.LENGTH_LONG).show();
                }
            } else {
                Intent intent = new Intent(CozyActivity.this, CosiFileDownloadService.class);
                ArrayList<String> arrayList = new ArrayList<String>();
                arrayList.add(0, file.getId() + "");
                intent.putStringArrayListExtra("fileToDownload", arrayList);
                startService(intent);
            }
        }
    }
}

From source file:com.fanfou.app.opensource.service.DownloadService.java

@SuppressWarnings("unused")
private PendingIntent getInstallPendingIntent(final String fileName) {
    final String mimeType = MimeTypeMap.getSingleton()
            .getMimeTypeFromExtension(CommonHelper.getExtension(fileName));
    if (mimeType != null) {
        final Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(fileName)), mimeType);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        final PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
        return pi;
    }//from   w  ww .ja  v  a2 s.com
    return null;
}