DownloadManager(专门用来下载数据的一个类)的简单使用

从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作。Download Manager处理HTTP连接并监控连接中的状态变化以及系统重启来确保每一个下载任务顺利完成。

 

UI界面布局图:

 

 

DownloadManager(专门用来下载数据的一个类)的简单使用_第1张图片

 

 

 


功能:根据按钮实现相应的功能

 

1、这里用到一个工具类里的一个截图的方法


代码

 


public class BitmapThumbnailHelper {

 /**
  * 对图片进行二次采样,生成缩略图。放置加载过大图片出现内存溢出
  */
 public static Bitmap createThumbnail(byte[] data, int newWidth,
   int newHeight) {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeByteArray(data, 0, data.length, options);
  int oldWidth = options.outWidth;
  int oldHeight = options.outHeight;

  // Log.i("Helper", "--->oldWidth:" + oldWidth);
  // Log.i("Helper", "--->oldHeight:" + oldHeight);

  int ratioWidth = 0;
  int ratioHeight = 0;

  if (newWidth != 0 && newHeight == 0) {
   ratioWidth = oldWidth / newWidth;
   options.inSampleSize = ratioWidth;
   // Log.i("Helper", "--->ratioWidth:" + ratioWidth);

  } else if (newWidth == 0 && newHeight != 0) {
   ratioHeight = oldHeight / newHeight;
   options.inSampleSize = ratioHeight;
  } else {
   ratioHeight = oldHeight / newHeight;
   ratioWidth = oldWidth / newWidth;
   options.inSampleSize = ratioHeight > ratioWidth ? ratioHeight
     : ratioWidth;
  }
  options.inPreferredConfig = Config.ALPHA_8;
  options.inJustDecodeBounds = false;
  Bitmap bm = BitmapFactory
    .decodeByteArray(data, 0, data.length, options);
  return bm;
 }

 public static Bitmap createThumbnail(String pathName, int newWidth,
   int newHeight) {
  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(pathName, options);
  int oldWidth = options.outWidth;
  int oldHeight = options.outHeight;

  int ratioWidth = 0;
  int ratioHeight = 0;

  if (newWidth != 0 && newHeight == 0) {
   ratioWidth = oldWidth / newWidth;
   options.inSampleSize = ratioWidth;
  } else if (newWidth == 0 && newHeight != 0) {
   ratioHeight = oldHeight / newHeight;
   options.inSampleSize = ratioHeight;
  } else {
   ratioHeight = oldHeight / newHeight;
   ratioWidth = oldWidth / newWidth;
   options.inSampleSize = ratioHeight > ratioWidth ? ratioHeight
     : ratioWidth;
  }
  options.inPreferredConfig = Config.ALPHA_8;
  options.inJustDecodeBounds = false;
  Bitmap bm = BitmapFactory.decodeFile(pathName, options);
  return bm;
 }

 // 获取视频文件的典型帧作为封面
 @SuppressLint("NewApi")
 public static Bitmap createVideoThumbnail(String filePath) {
  Bitmap bitmap = null;
  MediaMetadataRetriever retriever = new MediaMetadataRetriever();
  try {
   File file = new File(filePath);
   if (file.exists()) {
    Log.i("MainActivity", "文件存在:" + file.exists());
   }
   retriever.setDataSource(filePath);
   bitmap = retriever.getFrameAtTime();
  } catch (Exception ex) {
   ex.printStackTrace();
   Log.i("MainActivity", "" + ex.getMessage());
  } finally {
   try {
    retriever.release();
   } catch (RuntimeException ex) {
   }
  }
  return bitmap;
 }

 // 获取音乐文件中内置的专辑图片
 @SuppressLint("NewApi")
 public static Bitmap createAlbumThumbnail(String filePath) {
  Bitmap bitmap = null;
  MediaMetadataRetriever retriever = new MediaMetadataRetriever();
  try {
   retriever.setDataSource(filePath);
   byte[] art = retriever.getEmbeddedPicture();
   bitmap = BitmapFactory.decodeByteArray(art, 0, art.length);
  } catch (Exception ex) {
  } finally {
   try {
    retriever.release();
   } catch (RuntimeException ex) {
   }
  }
  return bitmap;
 }
}

 

 

 


2、在配置清单添加下载的相应权限


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
 
 
 
 
 
 
 
 
 
3、MainActivitiy.java类


代码


public class MainActivity extends Activity {

 private String downURL = "http://192.168.10.45:8080/myweb/service_aidl.wmv";
 private DownloadManager manager;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // DownloadManager属于系统服务
  // 获取管理器
  manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
 }

 // 下载视频按钮事件监听
 public void downmovie(View view) {
  switch (view.getId()) {
  case R.id.bt_download:
   // 下载视频方法
   download();
   break;
  case R.id.bt_downmovie:
   Intent intent = new Intent();
   intent.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);// 打开下载的所有视频
   startActivity(intent);
   break;
  case R.id.bt_movie_cover:// 生成视频封面
   Bitmap bitmap = BitmapThumbnailHelper
     .createVideoThumbnail(Environment
       .getExternalStoragePublicDirectory(
         Environment.DIRECTORY_MOVIES)
       .getAbsolutePath()
       + File.separator + "service_aidl.wmv");
   ImageView imageView = (ImageView) this.findViewById(R.id.imageview);
   imageView.setImageBitmap(bitmap);
   break;
  }
 }

 private void download() {
  // 创建下载任务请求
  Request request = new Request(Uri.parse(downURL));

  request.setTitle("正在下载...");// 设置下载通知进度标题
  request.setDescription("service_aidl.wmv");// 设置通知进度条的正下方内容
  // request.setDescription(null); 设置为空 则显示下载剩余时间

  // 第一个参数:视频下载到哪里存放的位置
  // 第二个参数:存放的文件夹名字
  request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MOVIES,
    "service_aidl.wmv");
  // 设置下载模式
  request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

  // 把下载任务放到队列中
  manager.enqueue(request);
 }
}

 

 

总结:DownloadManager类把通知也封装了 对于下载比自己写个服务来下载还是比较好用的

你可能感兴趣的:(DownloadManager)