DownloadManager(專門用來下載數據的一個類)的簡單使用

從Android 2.3(API level 9)開始Android用系統服務(Service)的方式提供了Download Manager來優化處理長時間的下載操做。Download Manager處理HTTP鏈接並監控鏈接中的狀態變化以及系統重啓來確保每個下載任務順利完成。java

 

UI界面佈局圖:android

 

 

 

 

 


功能:根據按鈕實現相應的功能web

 

一、這裏用到一個工具類裏的一個截圖的方法ide


代碼工具

 


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);this

  int ratioWidth = 0;
  int ratioHeight = 0;code

  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;
 }
}

 

 

 


二、在配置清單添加下載的相應權限


    <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"/>
 
 
 
 
 
 
 
 
 
三、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類把通知也封裝了 對於下載比本身寫個服務來下載仍是比較好用的

相關文章
相關標籤/搜索