Search in sources :

Example 31 with MimeTypeMap

use of android.webkit.MimeTypeMap in project smartmodule by carozhu.

the class DownloadService method onStartCommand.

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    if (!blnPermission) {
        return super.onStartCommand(intent, flags, startId);
    }
    if (intent != null) {
        Bundle bundle = intent.getExtras();
        String downloadUrl = bundle.getString("downloadUrl");
        fileName = bundle.getString("fileName");
        appDowunPath = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
        //Log.i("dirType","onStartCommand appDowunPath == "+appDowunPath +  " fileName == "+fileName);
        File downloadFile = new File(appDowunPath + "/" + fileName);
        if (downloadFile.exists()) {
            installAPK(downloadFile);
            return super.onStartCommand(intent, flags, startId);
        }
        //创建下载任务,downloadUrl就是下载链接
        request = new DownloadManager.Request(Uri.parse(downloadUrl));
        //设置下载网络现在方式
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        //漫游网络是否可以下载
        request.setAllowedOverRoaming(true);
        /*
             * 设置允许使用的网络类型, 可选值:
             * NETWORK_MOBILE:      移动网络
             * NETWORK_WIFI:        WIFI网络
             * NETWORK_BLUETOOTH:   蓝牙网络
             * 默认为所有网络都允许
             */
        // request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        //设置带通知栏下载
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setVisibleInDownloadsUi(true);
        //更多路径下载设置参考:http://www.cnblogs.com/zhaoyanjun/p/4591960.html
        //设置路径有多种方式 指定下载路径和下载文件名(设置下载文件的保存位置)
        // 1 TYPE
        //request.setDestinationInExternalFilesDir(this, null, mApkDir);//自己制定下载路径
        // 2 TYPE
        //File saveFile = new File(Environment.getExternalStorageDirectory(),fileName+".apk");
        // 3 TYPE
        //request.setDestinationUri(Uri.fromFile(saveFile));
        // 4 TYPE
        //request.setDestinationInExternalPublicDir(dirType, fileName);
        // 5 TYpe 设置到app下载目录
        String appDownloadPath = Environment.DIRECTORY_DOWNLOADS;
        //目录: Android -> data -> com.app -> files -> Download
        request.setDestinationInExternalFilesDir(this, appDownloadPath, fileName);
        /*在默认的情况下,通过Download Manager下载的文件是不能被Media Scanner扫描到的 。
            进而这些下载的文件(音乐、视频等)就不会在Gallery 和  Music Player这样的应用中看到。
            为了让下载的音乐文件可以被其他应用扫描到,我们需要调用Request对象的
            */
        request.allowScanningByMediaScanner();
        //设置请求的Mime http://www.jianshu.com/p/7ad92b3d9069
        MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
        request.setMimeType(mimeTypeMap.getMimeTypeFromExtension(downloadUrl));
        //将下载任务加入下载队列,否则不会进行下载
        mTaskId = mDownloadManager.enqueue(request);
        /*
           参考来自:http://www.cnblogs.com/zhaoyanjun/p/4591960.html
           下载管理器中有很多下载项,怎么知道一个资源已经下载过,避免重复下载呢?
           我的项目中的需求就是apk更新下载,用户点击更新确定按钮,第一次是直接下载,
           后面如果用户连续点击更新确定按钮,就不要重复下载了。
           可以看出来查询和操作数据库查询一样的
           */
        /*验证没查询到,待解决
            DownloadManager.Query query = new DownloadManager.Query();
            query.setFilterById(mTaskId);
            Cursor cursor = mDownloadManager.query(query);
            if (!cursor.moveToFirst()) {// 没有记录

            } else {

            }
            */
        //注册广播接收者,监听下载状态,通知栏点击状态
        registerReceiver(new DownloadManagerReceiver(), new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
    //return super.onStartCommand(intent, flags, startId);
    return Service.START_STICKY;
}
Also used : IntentFilter(android.content.IntentFilter) Bundle(android.os.Bundle) MimeTypeMap(android.webkit.MimeTypeMap) File(java.io.File) DownloadManager(android.app.DownloadManager)

Example 32 with MimeTypeMap

use of android.webkit.MimeTypeMap in project instructure-android by instructure.

the class FileUploadUtils method getMimeTypeFromFileNameWithExtension.

public static String getMimeTypeFromFileNameWithExtension(String fileNameWithExtension) {
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    int index = fileNameWithExtension.indexOf(".");
    String ext = "";
    if (index != -1) {
        // Add one so the dot isn't included
        ext = fileNameWithExtension.substring(index + 1).toLowerCase();
    }
    return mime.getMimeTypeFromExtension(ext);
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap) SuppressLint(android.annotation.SuppressLint)

Example 33 with MimeTypeMap

use of android.webkit.MimeTypeMap in project instructure-android by instructure.

the class FileUtilities method getMimeType.

public static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 34 with MimeTypeMap

use of android.webkit.MimeTypeMap in project instructure-android by instructure.

the class FileUtils method getMimeType.

public static String getMimeType(String url) {
    String type = null;
    String extension = MimeTypeMap.getFileExtensionFromUrl(url);
    if (extension != null) {
        MimeTypeMap mime = MimeTypeMap.getSingleton();
        type = mime.getMimeTypeFromExtension(extension);
    }
    return type;
}
Also used : MimeTypeMap(android.webkit.MimeTypeMap)

Example 35 with MimeTypeMap

use of android.webkit.MimeTypeMap in project instructure-android by instructure.

the class FileUploadUtils method getMimeTypeFromFileNameWithExtension.

public static String getMimeTypeFromFileNameWithExtension(String fileNameWithExtension) {
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    int index = fileNameWithExtension.indexOf(".");
    String ext = "";
    if (index != -1) {
        // Add one so the dot isn't included
        ext = fileNameWithExtension.substring(index + 1).toLowerCase();
    }
    return mime.getMimeTypeFromExtension(ext);
}
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