东西拼凑,最终弄出来能够用的代码
[1].[代码] [Java]代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | public class DownManagerTask { private static final String TAG = "DownManagerTask" ; private static DownManagerTask DOWN_TASK = new DownManagerTask(); private Context context; private DownloadManager downloadManager; long downloadId = - 1 ; private Bitmap bitmap; private ImageView imageView; private DownManagerTask(){} public static DownManagerTask getInstance(){ return DOWN_TASK; } @SuppressLint ( "InlinedApi" ) public void init(Context context,ImageView imageView){ this .context = context; this .imageView = imageView; downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); } /** * 将下载的图片转化成bitMap * */ @SuppressLint ( "NewApi" ) private void parseBitMap(){ try { ParcelFileDescriptor pfd = downloadManager.openDownloadedFile(downloadId); FileDescriptor fileDescriptor = pfd.getFileDescriptor(); bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor); pfd.close(); } catch (Exception e){ e.printStackTrace(); } } /** * 下载方法 * @param url */ @SuppressLint ( "NewApi" ) public void download(String url){ Log.d(TAG, url); Uri uri = Uri.parse(url); DownloadManager.Request request = new DownloadManager.Request(uri); request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI); request.setAllowedOverRoaming( false ); MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton(); String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap .getFileExtensionFromUrl(url)); request.setMimeType(mimeString); //request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Bugdroid.png"); request.allowScanningByMediaScanner(); request.setVisibleInDownloadsUi( true ); downloadId = downloadManager.enqueue(request); Log.d(TAG, "downloadId:" +downloadId); context.registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); } public void unregisterReceiver(){ context.unregisterReceiver(receiver); } @SuppressLint ( "NewApi" ) public void temp(){ Cursor c = downloadManager.query( new DownloadManager.Query().setFilterById(downloadId)); if (c != null ){ c.moveToFirst(); Log.d(TAG,(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)))); c.close(); } } private BroadcastReceiver receiver = new BroadcastReceiver() { @SuppressLint ( "NewApi" ) @Override public void onReceive(Context context, Intent intent) { //这里能够取得下载的id,这样就能够知道哪个文件下载完毕了。适用与多个下载任务的监听 if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){ Log.v(TAG, "" +intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0 )); Log.v(TAG, "download complete" ); parseBitMap(); imageView.setImageBitmap(bitmap); Toast.makeText(context, "download complete" , Toast.LENGTH_SHORT).show(); } } }; |