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.none.tom.simplerssreader.feed.CurrentFeed.java

public static List<String> getEnclosureForEntry(final SyndEntry entry) {
    final SyndEnclosure enclosure = entry.getEnclosures().get(0);

    return new ArrayList<>(Arrays.asList(enclosure.getUrl(), FileUtils.parseFileSize(enclosure.getLength()),
            MimeTypeMap.getSingleton().getExtensionFromMimeType(enclosure.getType())));
}

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

void sendMultiple(final ArrayList<FilesListFragment.DecryptArgHolder> args) {
    new Thread(new Runnable() {
        @Override//from w ww  . j a  va  2s  . co m
        public void run() {
            ArrayList<Uri> uris = new ArrayList<Uri>();
            Set<String> mimes = new HashSet<String>();
            MimeTypeMap myMime = MimeTypeMap.getSingleton();
            for (FilesListFragment.DecryptArgHolder arg : args) {
                File tempFile = getFile(arg.encryptedFile, arg.onFinish);
                //File specified is not invalid
                if (tempFile != null) {
                    if (tempFile.getParentFile().equals(Storage.getTempFolder()))
                        tempFile = new java.io.File(Storage.getTempFolder(), tempFile.getName());
                    uris.add(OurFileProvider.getUriForFile(context, OurFileProvider.FILE_PROVIDER_AUTHORITY,
                            tempFile));
                    mimes.add(myMime.getMimeTypeFromExtension(arg.encryptedFile.getType()));

                }
            }
            if (uris.size() == 0 || mimes.size() == 0)
                return;
            Intent newIntent;
            if (uris.size() == 1) {
                newIntent = new Intent(Intent.ACTION_SEND);
                newIntent.putExtra(Intent.EXTRA_STREAM, uris.get(0));
            } else {
                newIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
                newIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            }
            if (mimes.size() > 1)
                newIntent.setType("text/plain"); //Mixed filetypes
            else
                newIntent.setType(new ArrayList<String>(mimes).get(0));
            newIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

            Intent chooserIntent = generateCustomChooserIntent(newIntent, uris);
            try {
                startActivity(Intent.createChooser(chooserIntent,
                        CustomApp.context.getString(R.string.Dialog__send_file)));
                FilesActivity.onPauseDecision.startActivity();
            } catch (android.content.ActivityNotFoundException e) {
                Util.toast(context, CustomApp.context.getString(R.string.Error__no_activity_view),
                        Toast.LENGTH_LONG);
                FilesActivity.onPauseDecision.finishActivity();
            }
        }
    }).start();
}

From source file:com.android.browser.FetchUrlMimeType.java

@Override
public void onPostExecute(ContentValues values) {
    final String mimeType = values.getAsString("Content-Type");
    final String contentDisposition = values.getAsString("Content-Disposition");
    if (mimeType != null) {
        String url = mValues.getAsString(Downloads.Impl.COLUMN_URI);
        if (mimeType.equalsIgnoreCase("text/plain") || mimeType.equalsIgnoreCase("application/octet-stream")) {
            String newMimeType = MimeTypeMap.getSingleton()
                    .getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
            if (newMimeType != null) {
                mValues.put(Downloads.Impl.COLUMN_MIME_TYPE, newMimeType);
            }//from   w  w  w. ja v a2s .com
        }
        String filename = URLUtil.guessFileName(url, contentDisposition, mimeType);
        mValues.put(Downloads.Impl.COLUMN_FILE_NAME_HINT, filename);
    }

    // Start the download
    final Uri contentUri = mActivity.getContentResolver().insert(Downloads.Impl.CONTENT_URI, mValues);
}

From source file:com.github.snowdream.android.apps.imageviewer.ImageViewerActivity.java

public void initData() {
    imageLoader = ImageLoader.getInstance();
    imageUrls = new ArrayList<String>();

    Intent intent = getIntent();// w w w.ja v  a  2 s.  c  o m
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            imageUrls = bundle.getStringArrayList(Extra.IMAGES);
            imagePosition = bundle.getInt(Extra.IMAGE_POSITION, 0);
            imageMode = bundle.getInt(Extra.IMAGE_MODE, 0);
            Log.i("The snowdream bundle path of the image is: " + imageUri);
        }

        Uri uri = (Uri) intent.getData();
        if (uri != null) {
            imageUri = uri.getPath();
            fileName = uri.getLastPathSegment();
            getSupportActionBar().setSubtitle(fileName);
            Log.i("The path of the image is: " + imageUri);

            File file = new File(imageUri);

            imageUri = "file://" + imageUri;
            File dir = file.getParentFile();
            if (dir != null) {
                FileFilter fileFilter = new FileFilter() {
                    @Override
                    public boolean accept(File f) {
                        if (f != null) {
                            String extension = MimeTypeMap
                                    .getFileExtensionFromUrl(Uri.encode(f.getAbsolutePath()));
                            if (!TextUtils.isEmpty(extension)) {
                                String mimeType = MimeTypeMap.getSingleton()
                                        .getMimeTypeFromExtension(extension);
                                if (!TextUtils.isEmpty(mimeType) && mimeType.contains("image")) {
                                    return true;
                                }
                            }
                        }
                        return false;
                    }
                };

                File[] files = dir.listFiles(fileFilter);

                if (files != null && files.length > 0) {
                    int size = files.length;

                    for (int i = 0; i < size; i++) {
                        imageUrls.add("file://" + files[i].getAbsolutePath());
                    }
                    imagePosition = imageUrls.indexOf(imageUri);
                    imageMode = 1;
                    Log.i("Image Position:" + imagePosition);
                }

            } else {
                imageUrls.add("file://" + imageUri);
                imagePosition = 0;
                imageMode = 0;
            }
        }
    }

    else

    {
        Log.w("The intent is null!");
    }

}

From source file:com.apptentive.android.sdk.model.FileMessage.java

public boolean createStoredFile(Context context, String uriString) {
    Uri uri = Uri.parse(uriString);/*from  www.j a  v  a  2  s.c  om*/

    ContentResolver resolver = context.getContentResolver();
    String mimeType = resolver.getType(uri);
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String extension = mime.getExtensionFromMimeType(mimeType);

    // If we can't get the mime type from the uri, try getting it from the extension.
    if (extension == null) {
        extension = MimeTypeMap.getFileExtensionFromUrl(uriString);
    }
    if (mimeType == null && extension != null) {
        mimeType = mime.getMimeTypeFromExtension(extension);
    }

    setFileName(uri.getLastPathSegment() + "." + extension);
    setMimeType(mimeType);

    InputStream is = null;
    try {
        is = new BufferedInputStream(context.getContentResolver().openInputStream(uri));
        return createStoredFile(context, is, mimeType);
    } catch (FileNotFoundException e) {
        Log.e("File not found while storing file.", e);
    } catch (IOException e) {
        Log.a("Error storing image.", e);
    } finally {
        Util.ensureClosed(is);
    }
    return false;
}

From source file:com.doplgangr.secrecy.utils.Util.java

public static String getFileTypeFromExtension(String extension) {
    if (extension.length() > 0) {
        return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    }/*from   ww  w .  j ava 2 s  . co  m*/
    return "application/octet-stream";
}

From source file:com.raspberry.library.util.IntentUtils.java

/**
 * ?App(?6.0)?//from  w w w  .j ava  2 s . co m
 *
 * @param file 
 * @return intent
 */
public static Intent getInstallAppIntent(File file) {
    if (file == null)
        return null;
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String type;
    if (Build.VERSION.SDK_INT < 23) {
        type = "application/vnd.android.package-archive";
    } else {
        type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(FileUtils.getFileExtension(file));
    }
    intent.setDataAndType(Uri.fromFile(file), type);
    return intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

public static String resolveMimeType(String fileName) {
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(FilenameUtils.getExtension(fileName));
    if (mimeType == null) {
        if (isContainer(fileName)) {
            mimeType = "application/zip";
        }//from   ww  w.j a v  a2 s. co  m
    }
    return mimeType;
}

From source file:com.google.android.apps.forscience.whistlepunk.PictureUtils.java

public static void launchExternalViewer(Activity activity, String fileUrl) {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl);
    String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
    if (!TextUtils.isEmpty(type)) {
        String filePath = fileUrl;
        if (filePath.startsWith("file:")) {
            filePath = filePath.substring("file:".length());
        }/*from  w  w  w . j  a  v  a2s . c o m*/
        Uri photoUri = FileProvider.getUriForFile(activity, activity.getPackageName(), new File(filePath));
        intent.setDataAndType(photoUri, type);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            // Needed to avoid security exception on KitKat.
            intent.setClipData(ClipData.newRawUri(null, photoUri));
        }
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        try {
            activity.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Log.e(TAG, "No activity found to handle this " + fileUrl + " type " + type);
        }
    } else {
        Log.w(TAG, "Could not find mime type for " + fileUrl);
    }
}

From source file:org.zywx.wbpalmstar.engine.EDownloadDialog.java

private void saveToFile() throws Exception {
    mInStream = mConnection.getInputStream();
    if (mInStream == null) {
        return;/* w w  w .  j  av  a2 s.c  o m*/
    }
    if (contentLength <= 0) {
        String cLength = mConnection.getHeaderField("Content-Length");
        if (cLength != null) {
            contentLength = Long.parseLong(cLength);
        }
    }
    MimeTypeMap mtm = MimeTypeMap.getSingleton();
    String extension = mtm.getExtensionFromMimeType(mimetype);
    File tm = Environment.getExternalStorageDirectory();
    File target = new File(tm.getAbsoluteFile() + "/Download/");
    if (!target.exists()) {
        target.mkdirs();
    }
    mTmpFile = File.createTempFile("/Download/", "." + extension, tm);
    OutputStream outStream = new FileOutputStream(mTmpFile);
    byte buffer[] = new byte[1024 * 3];
    while (true) {
        int numread = mInStream.read(buffer);
        if (numread == -1) {
            mProgressHandler.sendEmptyMessage(100);
            break;
        }
        outStream.write(buffer, 0, numread);
        downLoaderSise += numread;
        int p = (int) (((float) downLoaderSise / contentLength) * 100);
        mProgressHandler.sendEmptyMessage(p);
    }
    if (contentLength <= 0) {
        mProgressHandler.sendEmptyMessage(100);
    }
}