将图片文件保存到相册

1.复制单个文件


public static boolean copyFile(String srcPath, String destPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(srcPath);
if (oldfile.exists() && oldfile.isFile()) { // 文件存在时
InputStream inStream = new FileInputStream(srcPath); // 读入原文件
FileOutputStream fs = new FileOutputStream(destPath);


Utils.LOGD(TAG, "  copy:" + srcPath + " >  " + destPath);


byte[] buffer = new byte[4096];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
fs.write(buffer, 0, byteread);
}
inStream.close();
Utils.LOGD(TAG, "            OK");
return true;
} else {
return false;
}
} catch (Exception e) {
Utils.LOGE("FileUtil", "复制单个文件操作出错");
e.printStackTrace();
return false;
}
}

2.发送通知

图片文件保存到相册,并且通知相册信息发生改变

Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);//这个广播的目的就是更新图库

你可能感兴趣的:(将图片文件保存到相册)