Android设备从外置TF卡文件夹中复制指定文件到到另手机内存文件夹中

TF卡视频文件监听与复制
本文介绍了一种通过监听TF卡插入状态并查找特定文件夹的方法,详细展示了如何使用广播接收器监听TF卡的插入与移除,并查找指定文件夹下的视频文件,最后实现了将这些视频文件复制到另一个指定目录的过程。

1.通过广播监听TF卡插入状态

 IntentFilter intentFilter = new IntentFilter();
 intentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
 intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
 intentFilter.setPriority(2147483647);
 intentFilter.addDataScheme("file");
 registerReceiver(SDcardReceiver, intentFilter);

如果存在TF卡就查找TF卡中指定文件夹是否存在

private BroadcastReceiver SDcardReceiver = new BroadcastReceiver(){  
        @Override  
        public void onReceive(Context context, Intent intent) {  
            String action = intent.getAction();  
            if(action.equals(Intent.ACTION_MEDIA_MOUNTED))
            {  
            	Toast.makeText(CopyService.this, "插入了SD卡", Toast.LENGTH_LONG).show();
            	if(HelpCommon.isDirExist("UPDATA"))
            	{
            		Toast.makeText(CopyService.this, "TF卡中找到了VideoData文件夹,将进行拷贝!", Toast.LENGTH_LONG).show();
            		Intent i = new Intent(CopyService.this, MainActivity.class);
            		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            		startActivity(i);
            	}else{
            		Toast.makeText(CopyService.this, "TF卡中没有找到了VideoData文件夹", Toast.LENGTH_LONG).show();
            	}
            }
            else
            {
                Toast.makeText(CopyService.this, "没有SD卡", Toast.LENGTH_LONG).show();
            } 
        }  
    };


如果存在就查找你想复制的文件类型

//要复制的目录,视频信息集合
	public static List<VideoInfo> mAllVideoList = null;
	public static HashMap<String, Boolean> mHashMapExterns;
	

	private void initHashMap() {
		mHashMapExterns = new HashMap<String, Boolean>();
		mHashMapExterns.put(".mp4", true);
		mHashMapExterns.put(".3gp",true);
		mHashMapExterns.put(".wmv",true);
		mHashMapExterns.put(".ts",true);
		mHashMapExterns.put(".rmvb",true);
		mHashMapExterns.put(".mov",true);
		mHashMapExterns.put(".m4v",true);
		mHashMapExterns.put(".avi",true);
		mHashMapExterns.put(".m3u8",true);
		mHashMapExterns.put(".3gpp",true);
		mHashMapExterns.put(".3gpp2",true);
		mHashMapExterns.put(".mkv",true);
		mHashMapExterns.put(".flv",true);
		mHashMapExterns.put(".divx",true);
		mHashMapExterns.put(".f4v",true);
		mHashMapExterns.put(".rm",true);
		mHashMapExterns.put(".asf",true);
		mHashMapExterns.put(".ram",true);
		mHashMapExterns.put(".mpg",true);
		mHashMapExterns.put(".v8",true);
		mHashMapExterns.put(".swf",true);
		mHashMapExterns.put(".m2v",true);
		mHashMapExterns.put(".asx",true);
		mHashMapExterns.put(".ra",true);
		mHashMapExterns.put(".ndivx",true);
		mHashMapExterns.put(".xvid",true);
	}

	
  /**
   * 获取指定文件夹内的视频文件
   * @param list
   * @param file
   */
  private static void getVideoFile(final List<VideoInfo> list, File file) {// 获得视频文件
		file.listFiles(new FileFilter() {
			@Override
			public boolean accept(File file) {
				//sdCard找到视频名称
				String name = file.getName();
				int i = name.lastIndexOf('.');
				if (i != -1) {
					name = name.substring(i);
					if (mHashMapExterns.containsKey(name.toLowerCase())) {
						VideoInfo vi = new VideoInfo();
						vi.setLength(file.length());
						vi.setDisplayName(file.getName());
						vi.setPath(file.getAbsolutePath());
						list.add(vi);
						return true;
					}
				} else if (file.isDirectory()) {
					getVideoFile(list, file);
				}
				return false;
			}
		});
	}


2.复制到指定目录

首先判断指定的文加件是否存在,不存在就创建

/* 
	 * 判断SD卡指定目录是否存在 
	 */  
	public static boolean isDirExist(String dir)
   {  
	    File file = new File(SDCardRoot + dir + File.separator);  
        if(file.exists())  
        {
    	    return true;
        }
        return false;
        /*else{
    	    file.mkdir();  //如果不存在则创建  
    	    return false;
        }   */
   }

开始复制文件

 /**
   * 管道对管道 文件拷贝
   * @param oldPath
   * @param newPath
   */
  @SuppressWarnings("resource")
  public static void ForChannel(final File oldPath,final File newPath)
  {
 new Thread(new Runnable() 
 {
@Override
public void run() 
{
  try {
      int length = 2097152;
  FileInputStream in = new FileInputStream(oldPath);
  FileOutputStream out = new FileOutputStream(newPath);
  FileChannel inC = in.getChannel();
  FileChannel outC = out.getChannel();
  ByteBuffer b = null;

  while (true) 
  {
  System.out.println(inC.position()+"-----"+inC.size());
if (inC.position() == inC.size()) 
{
inC.close();
outC.close();
}
if ((inC.size() - inC.position()) < length) 
{
length = (int) (inC.size() - inC.position());
} else
length = 2097152;
b = ByteBuffer.allocateDirect(length);
inC.read(b);
b.flip();
outC.write(b);
outC.force(false);
      }
} catch (Exception e) {
e.printStackTrace();
}
}
  }).start();
   }


下载地址: Demo下载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值