zoukankan      html  css  js  c++  java
  • Android中显示sd卡的图片和视频

    ImageView显示sd卡的图片很简单,只要知道了图片的Path,就可以引用图片的地址,用ImageView来显示

    path = Environment.getExternalStorageDirectory() ; //获得SDCard目录
    Bitmap bmp;
    ImageView  iv = (ImageView) contentView.findViewById(R.id.x);
    if(bmp==null)
           bmp = BitmapFactory.decodeFile(path+"xx.jpg",null);
    iv.setImageBitmap(bmp);

    注意:"xx.jpg",是你图片的name

     

    1.判断SD卡是否存在:
              String status = Environment.getExternalStorageState();
                 if (status.equals(Environment.MEDIA_MOUNTED)){
                    //SD卡存在
                  }else{
                     //SD卡不存在
                     }

    2.获取SD卡根目录的两种方式:
        (1)SD卡根目录的路径一般为"/mnt/sdcard"。
          File file = new File("/mnt/sdcard");
          (2)另外一种获取SD卡根目录的方式相对灵活。   
          File file = Environment.getExternalStorageDirectory();

    3.进入SD卡中指定的文件夹:
          比如:Recorder文件夹。
          File file = new File("/mnt/sdcard/Recorder");

    4.将指定类型文件添加到集合:
          以图片文件为例:
           File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++){
                  //文件类对象,类中保存文件的信息,如路径,bitmap,文件类型            
                   MediaInfo mediaInfo = new MediaInfo();
                   //将文件对象添加到集合中
                  list.add(mediaInfo);
                  filePath = files.getPath().toString();
                  if (filePath.endsWith(".png") ||filePath.endsWith(".jpg") ){
                      //将图片路径添加到集合中,方便获取
                        liist.get(i).setmPath(filePath);                       
                     }
            }

    4.判断是文件或是文件夹:
          if (files.isFile()){

              //是文件
           }else{
              //是文件夹
                   }

    5.获取图片文件的bitmap:
           if (filePath.contains(".jpg") || filePath.contains(".png")) {
        //将图片压缩,避免内存溢出的两种方式
        (1)BitmapFactory.Options options = new BitmapFactory.Options();
                 //压缩图片
                options.inSampleSize = 5;
                mBitmap = BitmapFactory.decodeFile(path, options);
        (2)if (mBitmap != null) {
                mBitmap = BitmapFactory.decodeFile(path);
               // 将原来的位图缩小
               mSmallBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 180, true);            
               mBitmap.recycle();// 释放内存
                if (mSmallBitmap != null) {
                // 可在此将图片文件的视图添加于界面中
                publishProgress(new MediaInfo(mSmallBitmap));
              }
                list.get(i).setmBitmap(mSmallBitmap);
           }

    6.获取视频文件缩略图:
           if (filePath.contains(".jpg") || filePath.contains(".png")) {
                  //参数为视频文件的路径,以及缩略图的宽度和高度
                   Bitmap tumb = getVideoTumb(filePath, 200, 180);
                }
               Bitmap getVideoTumb(String path, int width, int height) {
               // Thumbnails.MINI_KIND取出的图标较小.
               Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MINI_KIND);

        if (bitmap == null) {
                   return bitmap;
                 }
                Bitmap bitmapTumb = ThumbnailUtils.extractThumbnail(bitmap, width, height);
                bitmap.recycle();
                return bitmapTumb;
           }
    7.获取视频文件时长:
               // 传入的参数为视频文件路径
               void  getMediaDuration(String path) {
               MediaPlayer mMediaPlayer = new MediaPlayer();
               mMediaPlayer.setDataSource(path);
               mMediaPlayer.prepare();
               //获得视频时长的毫秒数duration
               int duration = mMediaPlayer.getDuration();
              
    }

     

  • 相关阅读:
    Oracle Function REGEXP
    Asp.net 注册IIS
    SAP모듈
    C# 程序中,不使用TNS File 中的服务名进行数据库连接
    跟我一起学XNA(2)让物体动起来②(附源码)
    ubuntu显卡的几个帖子
    每秒改变一次背景颜色以及由此引发的一些有趣的小事情(.net方向)
    to myself
    vi编辑器上下左右键盘变成ABCD
    一个最简单的linux hello world模块
  • 原文地址:https://www.cnblogs.com/all88/p/2995364.html
Copyright © 2011-2022 走看看