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下载