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:mobisocial.musubi.objects.FileObj.java

public static Obj from(Context context, Uri dataUri) throws IOException {
    //TODO: is this the proper way to do it?
    if (dataUri == null) {
        throw new NullPointerException();
    }/*from   w  w  w.  j  a  va 2s .c om*/
    ContentResolver cr = context.getContentResolver();
    InputStream in = cr.openInputStream(dataUri);
    long length = in.available();

    String ext;
    String mimeType;
    String filename;

    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    if ("content".equals(dataUri.getScheme())) {
        ContentResolver resolver = context.getContentResolver();
        mimeType = resolver.getType(dataUri);
        ext = mimeTypeMap.getExtensionFromMimeType(mimeType);
        filename = "Musubi-" + sDateFormat.format(new Date());
    } else {
        ext = MimeTypeMap.getFileExtensionFromUrl(dataUri.toString());
        mimeType = mimeTypeMap.getMimeTypeFromExtension(ext);
        filename = Uri.parse(dataUri.toString()).getLastPathSegment();
        if (filename == null) {
            filename = "Musubi-" + sDateFormat.format(new Date());
        }
    }

    if (mimeType == null || mimeType.isEmpty()) {
        throw new IOException("Unidentified mime type");
    }

    if (ext == null || ext.isEmpty()) {
        ext = mimeTypeMap.getExtensionFromMimeType(mimeType);
    }

    if (!ext.isEmpty() && !filename.endsWith(ext)) {
        filename = filename + "." + ext;
    }

    if (mimeType.startsWith("video/")) {
        return VideoObj.from(context, dataUri, mimeType);
    } else if (mimeType.startsWith("image/")) {
        return PictureObj.from(context, dataUri, true);
    }

    if (length > EMBED_SIZE_LIMIT) {
        if (length > CORRAL_SIZE_LIMIT) {
            throw new IOException("file too large for push");
        } else {
            return fromCorral(context, mimeType, filename, length, dataUri);
        }
    } else {
        in = cr.openInputStream(dataUri);
        return from(mimeType, filename, length, IOUtils.toByteArray(in));
    }
}

From source file:com.owncloud.android.operations.DownloadFileOperation.java

public String getMimeType() {
    String mimeType = mFile.getMimetype();
    if (mimeType == null || mimeType.length() <= 0) {
        try {//ww w. j  a  v a 2s. co m
            mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                    mFile.getRemotePath().substring(mFile.getRemotePath().lastIndexOf('.') + 1));
        } catch (IndexOutOfBoundsException e) {
            Log_OC.e(TAG, "Trying to find out MIME type of a file without extension: " + mFile.getRemotePath());
        }
    }
    if (mimeType == null) {
        mimeType = "application/octet-stream";
    }
    return mimeType;
}

From source file:com.owncloud.android.ui.helpers.FileOperationsHelper.java

public void openFile(OCFile file) {
    if (file != null) {
        String storagePath = file.getStoragePath();

        Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
        intentForSavedMimeType.setDataAndType(file.getExposedFileUri(mFileActivity), file.getMimetype());

        intentForSavedMimeType//  ww  w .  j a v a  2  s  .  c om
                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        Intent intentForGuessedMimeType = null;
        if (storagePath.lastIndexOf('.') >= 0) {
            String guessedMimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
            if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
                intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
                intentForGuessedMimeType.setDataAndType(file.getExposedFileUri(mFileActivity), guessedMimeType);
                intentForGuessedMimeType.setFlags(
                        Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        Intent openFileWithIntent;
        if (intentForGuessedMimeType != null) {
            openFileWithIntent = intentForGuessedMimeType;
        } else {
            openFileWithIntent = intentForSavedMimeType;
        }

        List<ResolveInfo> launchables = mFileActivity.getPackageManager()
                .queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);

        if (launchables != null && launchables.size() > 0) {
            try {
                mFileActivity.startActivity(Intent.createChooser(openFileWithIntent,
                        mFileActivity.getString(R.string.actionbar_open_with)));
            } catch (ActivityNotFoundException anfe) {
                mFileActivity
                        .showSnackMessage(mFileActivity.getString(R.string.file_list_no_app_for_file_type));
            }
        } else {
            mFileActivity.showSnackMessage(mFileActivity.getString(R.string.file_list_no_app_for_file_type));
        }

    } else {
        Log_OC.e(TAG, "Trying to open a NULL OCFile");
    }
}

From source file:com.doplgangr.secrecy.views.FileViewer.java

void decrypt(final EncryptedFile encryptedFile, final Runnable onFinish) {
    new AsyncTask<EncryptedFile, Void, File>() {
        @Override/*ww w. ja va2 s . com*/
        protected File doInBackground(EncryptedFile... encryptedFiles) {
            return getFile(encryptedFile, onFinish);
        }

        @Override
        protected void onPostExecute(File tempFile) {
            if (tempFile != null) {
                if (tempFile.getParentFile().equals(Storage.getTempFolder())) {
                    tempFile = new File(Storage.getTempFolder(), tempFile.getName());
                }
                Uri uri = OurFileProvider.getUriForFile(context, OurFileProvider.FILE_PROVIDER_AUTHORITY,
                        tempFile);
                MimeTypeMap myMime = MimeTypeMap.getSingleton();
                Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW);
                String mimeType = myMime.getMimeTypeFromExtension(encryptedFile.getType());
                newIntent.setDataAndType(uri, mimeType);
                newIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                //altIntent: resort to using file provider when content provider does not work.
                Intent altIntent = new Intent(android.content.Intent.ACTION_VIEW);
                Uri rawuri = Uri.fromFile(tempFile);
                altIntent.setDataAndType(rawuri, mimeType);
                afterDecrypt(newIntent, altIntent);
            }
        }
    }.execute(encryptedFile);
}

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

public void openFile(OCFile file) {
    if (file != null) {
        String storagePath = file.getStoragePath();
        String encodedStoragePath = WebdavUtils.encodePath(storagePath);

        Intent intentForSavedMimeType = new Intent(Intent.ACTION_VIEW);
        intentForSavedMimeType.setDataAndType(Uri.parse("file://" + encodedStoragePath), file.getMimetype());
        intentForSavedMimeType//from w w w.  j a  va  2 s . c om
                .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

        Intent intentForGuessedMimeType = null;
        if (storagePath.lastIndexOf('.') >= 0) {
            String guessedMimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(storagePath.substring(storagePath.lastIndexOf('.') + 1));
            if (guessedMimeType != null && !guessedMimeType.equals(file.getMimetype())) {
                intentForGuessedMimeType = new Intent(Intent.ACTION_VIEW);
                intentForGuessedMimeType.setDataAndType(Uri.parse("file://" + encodedStoragePath),
                        guessedMimeType);
                intentForGuessedMimeType.setFlags(
                        Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        Intent openFileWithIntent;
        if (intentForGuessedMimeType != null) {
            openFileWithIntent = intentForGuessedMimeType;
        } else {
            openFileWithIntent = intentForSavedMimeType;
        }

        List<ResolveInfo> launchables = mFileActivity.getPackageManager()
                .queryIntentActivities(openFileWithIntent, PackageManager.GET_INTENT_FILTERS);

        if (launchables != null && launchables.size() > 0) {
            try {
                mFileActivity.startActivity(Intent.createChooser(openFileWithIntent,
                        mFileActivity.getString(R.string.actionbar_open_with)));
            } catch (ActivityNotFoundException anfe) {
                showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
            }
        } else {
            showNoAppForFileTypeToast(mFileActivity.getApplicationContext());
        }

    } else {
        Log_OC.wtf(TAG, "Trying to open a NULL OCFile");
    }
}

From source file:mobisocial.musubi.util.UriImage.java

private void initFromFile(Context context, Uri uri) {
    mPath = uri.getPath();/*from  w  w  w .ja v  a2s.  co  m*/
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    String extension = MimeTypeMap.getFileExtensionFromUrl(mPath);
    if (TextUtils.isEmpty(extension)) {
        // getMimeTypeFromExtension() doesn't handle spaces in filenames nor can it handle
        // urlEncoded strings. Let's try one last time at finding the extension.
        int dotPos = mPath.lastIndexOf('.');
        if (0 <= dotPos) {
            extension = mPath.substring(dotPos + 1);
        }
    }
    mContentType = mimeTypeMap.getMimeTypeFromExtension(extension);
    // It's ok if mContentType is null. Eventually we'll show a toast telling the
    // user the picture couldn't be attached.
}

From source file:mobisocial.bento.anyshare.ui.FeedItemListActivity.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent ret) {
    if (requestCode == REQUEST_PICK) {
        if (resultCode == RESULT_OK) {
            Log.d(TAG, ret.toString());/*ww  w.  ja v  a  2s .co m*/
            Uri uri = ret.getData();
            Intent intent = new Intent(this, PostActivity.class);
            intent.setAction(Intent.ACTION_SEND);
            String mimetype = getContentResolver().getType(uri);
            String ext = MimeTypeMap.getFileExtensionFromUrl(uri.toString());

            MimeTypeMap mime = MimeTypeMap.getSingleton();
            if (mimetype == null || mimetype.isEmpty()) {
                if (!ext.isEmpty()) {
                    mimetype = mime.getMimeTypeFromExtension(ext);
                }
            }

            String fname = uri.getLastPathSegment();
            if (ext.isEmpty()) {
                fname += "." + mime.getExtensionFromMimeType(mimetype);
            }

            intent.setType(mimetype);
            intent.putExtra(Intent.EXTRA_SUBJECT, fname);
            intent.putExtra(Intent.EXTRA_TEXT, "");
            intent.putExtra(Intent.EXTRA_STREAM, uri);

            startActivityForResult(intent, HomeActivity.REQUEST_VIEW);

        }
    }
    super.onActivityResult(requestCode, resultCode, ret);
}

From source file:com.ugedal.weeklyschedule.ListFragment.java

public static String getMimeType(String url) {
    String type = null;//www.  ja v  a2s.  co m
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }
    return type;
}