Search in sources :

Example 21 with MimeTypeMap

use of android.webkit.MimeTypeMap in project robolectric by robolectric.

the class ShadowMimeTypeMapTest method getSingletonShouldAlwaysReturnSameInstance.

@Test
public void getSingletonShouldAlwaysReturnSameInstance() {
    MimeTypeMap firstInstance = MimeTypeMap.getSingleton();
    MimeTypeMap secondInstance = MimeTypeMap.getSingleton();
    assertSame(firstInstance, secondInstance);
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap) Test(org.junit.Test)

Example 22 with MimeTypeMap

use of android.webkit.MimeTypeMap in project ETSMobile-Android2 by ApplETS.

the class MoodleCourseDetailsFragment method onActivityCreated.

@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);
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) Cursor(android.database.Cursor) MimeTypeMap(android.webkit.MimeTypeMap) DownloadManager(android.app.DownloadManager) Uri(android.net.Uri) ActivityNotFoundException(android.content.ActivityNotFoundException) File(java.io.File)

Example 23 with MimeTypeMap

use of android.webkit.MimeTypeMap in project ETSMobile-Android2 by ApplETS.

the class MoodleCourseDetailsFragment method downloadMoodleObject.

/**
 * Downloads an given Moodle item into the user's device.
 * @param item the item to be downloaded.
 */
@NeedsPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
void downloadMoodleObject(MoodleModuleContent item) {
    String url = item.getFileurl() + "&token=" + ApplicationManager.userCredentials.getMoodleToken();
    Uri uri = Uri.parse(url);
    DownloadManager.Request request = new DownloadManager.Request(uri);
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, item.getFilename());
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    MimeTypeMap mimetype = MimeTypeMap.getSingleton();
    String extension = FilenameUtils.getExtension(item.getFilename());
    request.setMimeType(mimetype.getMimeTypeFromExtension(extension));
    dm = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
    enqueue = dm.enqueue(request);
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap) Uri(android.net.Uri) DownloadManager(android.app.DownloadManager) NeedsPermission(permissions.dispatcher.NeedsPermission)

Example 24 with MimeTypeMap

use of android.webkit.MimeTypeMap in project FileDownloaderManager by arlyxiao.

the class DownloadDoneNotification method get_mime_type.

private String get_mime_type(String url) {
    String[] parts = url.split("\\.");
    String extension = parts[parts.length - 1];
    String type = null;
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 25 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)

Aggregations

MimeTypeMap (android.webkit.MimeTypeMap)51 File (java.io.File)12 Intent (android.content.Intent)7 DownloadManager (android.app.DownloadManager)6 Uri (android.net.Uri)6 IOException (java.io.IOException)4 SimpleDateFormat (java.text.SimpleDateFormat)4 ArrayList (java.util.ArrayList)4 SuppressLint (android.annotation.SuppressLint)3 ActivityNotFoundException (android.content.ActivityNotFoundException)3 ResolveInfo (android.content.pm.ResolveInfo)3 FileNotFoundException (java.io.FileNotFoundException)3 Date (java.util.Date)3 IntentFilter (android.content.IntentFilter)2 Cursor (android.database.Cursor)2 View (android.view.View)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 MalformedURLException (java.net.MalformedURLException)2 MultipartBody (okhttp3.MultipartBody)2