Search in sources :

Example 6 with MimeTypeMap

use of android.webkit.MimeTypeMap in project mobile-android by photo.

the class NewPhotoObserver method getMimeType.

/**
     * Get the mime type for the file
     * 
     * @param file
     * @return
     */
public static String getMimeType(File file) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).getPath());
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension.toLowerCase());
    }
    CommonUtils.debug(TAG, "File: %1$s; extension %2$s; MimeType: %3$s", file.getAbsolutePath(), extension, type);
    return type;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 7 with MimeTypeMap

use of android.webkit.MimeTypeMap in project qksms by moezbhatti.

the class UriImage method initFromFile.

private void initFromFile(Context context, Uri uri) {
    mPath = uri.getPath();
    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.
    buildSrcFromPath();
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 8 with MimeTypeMap

use of android.webkit.MimeTypeMap in project Signal-Android by WhisperSystems.

the class SaveAttachmentTask method constructOutputFile.

private File constructOutputFile(String contentType, long timestamp) throws IOException {
    File sdCard = Environment.getExternalStorageDirectory();
    File outputDirectory;
    if (contentType.startsWith("video/")) {
        outputDirectory = new File(sdCard.getAbsoluteFile() + File.separator + Environment.DIRECTORY_MOVIES);
    } else if (contentType.startsWith("audio/")) {
        outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + Environment.DIRECTORY_MUSIC);
    } else if (contentType.startsWith("image/")) {
        outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + Environment.DIRECTORY_PICTURES);
    } else {
        outputDirectory = new File(sdCard.getAbsolutePath() + File.separator + Environment.DIRECTORY_DOWNLOADS);
    }
    if (!outputDirectory.mkdirs())
        Log.w(TAG, "mkdirs() returned false, attempting to continue");
    MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
    String extension = mimeTypeMap.getExtensionFromMimeType(contentType);
    SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
    String base = "signal-" + dateFormatter.format(timestamp);
    if (extension == null)
        extension = "attach";
    int i = 0;
    File file = new File(outputDirectory, base + "." + extension);
    while (file.exists()) {
        file = new File(outputDirectory, base + "-" + (++i) + "." + extension);
    }
    return file;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 9 with MimeTypeMap

use of android.webkit.MimeTypeMap in project material-dialogs by afollestad.

the class FileChooserDialog method listFiles.

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;
}
Also used : ArrayList(java.util.ArrayList) MimeTypeMap(android.webkit.MimeTypeMap) File(java.io.File)

Example 10 with MimeTypeMap

use of android.webkit.MimeTypeMap in project UltimateRecyclerView by cymcsg.

the class DataUtil method openFileInSystem.

public static void openFileInSystem(String path, Context context) {
    try {
        MimeTypeMap myMime = MimeTypeMap.getSingleton();
        Intent newIntent = new Intent(Intent.ACTION_VIEW);
        String mimeType = myMime.getMimeTypeFromExtension(fileExt(path).substring(1));
        newIntent.setDataAndType(Uri.fromFile(new File(path)), mimeType);
        newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(newIntent);
    } catch (Exception e) {
        Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show();
    }
}
Also used : Intent(android.content.Intent) MimeTypeMap(android.webkit.MimeTypeMap) File(java.io.File)

Aggregations

MimeTypeMap (android.webkit.MimeTypeMap)19 Intent (android.content.Intent)5 File (java.io.File)5 DownloadManager (android.app.DownloadManager)3 ActivityNotFoundException (android.content.ActivityNotFoundException)2 IntentFilter (android.content.IntentFilter)2 ResolveInfo (android.content.pm.ResolveInfo)2 View (android.view.View)2 ArrayList (java.util.ArrayList)2 BroadcastReceiver (android.content.BroadcastReceiver)1 Context (android.content.Context)1 PackageManager (android.content.pm.PackageManager)1 Cursor (android.database.Cursor)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 AdapterView (android.widget.AdapterView)1 ExpandableListView (android.widget.ExpandableListView)1 ListView (android.widget.ListView)1 MoodleCoreCourse (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCoreCourse)1 MoodleCoreCourses (ca.etsmtl.applets.etsmobile.model.Moodle.MoodleCoreCourses)1