Java android如何对文件进行校验和
保存文件校验和的最简单方法是什么 在下载方法中,我从URL下载图片(通过字符串图像传递), 一切正常,但如果我使用我的testCheckSum方法(这里是注释行),我会得到异常(“此目录中没有这样的文件”),但我确信存在该文件 你们能帮帮我吗Java android如何对文件进行校验和,java,android,checksum,Java,Android,Checksum,保存文件校验和的最简单方法是什么 在下载方法中,我从URL下载图片(通过字符串图像传递), 一切正常,但如果我使用我的testCheckSum方法(这里是注释行),我会得到异常(“此目录中没有这样的文件”),但我确信存在该文件 你们能帮帮我吗 public class ImageCacher { public static Bitmap getImage(final Context con, final String Image)throws NoSuchAlgorith
public class ImageCacher {
public static Bitmap getImage(final Context con, final String Image)throws NoSuchAlgorithmException, Exception {
if (Patterns.WEB_URL.matcher(Image.trim()).matches()) {
if ((ImageCache.ifExist(Image))) {
Toast.makeText(con.getApplicationContext(), "Image is already in Cache", Toast.LENGTH_SHORT).show();
return ImageCache.showImage(Uri.parse(Image).getLastPathSegment());
} else {
new AsyncTask<Void, Void, Boolean>() {
protected Boolean doInBackground(Void... params) {
ImageCache.download(con.getApplicationContext(), Image);
return null;
}
}.execute();
Toast.makeText(con.getApplicationContext(), "Image has been downloaded", Toast.LENGTH_SHORT).show();
}
while (ImageCache.spaceInMB(ImageCache.filePathFolder) > 50) {
ImageCache.deleteLastModified(ImageCache.filePathFolder, con.getApplicationContext());
}
//doesnt work
//String s= testChecksum(new File (filepathfolder, (Uri.parse(Image).getLastPathSegment()).trim() )
return null;
}else Toast.makeText(con.getApplicationContext(), "Please insert valid Url adress", Toast.LENGTH_SHORT).show();
return null;
}
}
static String testChecksum(File outputF) throws IOException, NoSuchAlgorithmException
{
MessageDigest md5 = MessageDigest.getInstance("MD5");
FileInputStream fis = new FileInputStream (outputF);
byte[] data = new byte[1024];
int read = 0;
while ((read = fis.read(data)) != -1) {
md5.update(data, 0, read);
};
byte[] hashBytes = md5.digest();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < hashBytes.length; i++) {
sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
}
String fileHash = sb.toString();
return fileHash;
}
公共类图像缓存器{
公共静态位图getImage(最终上下文con,最终字符串图像)抛出NoSuchAlgorithmException,Exception{
if(Patterns.WEB_URL.matcher(Image.trim()).matches()){
if((ImageCache.ifExist(Image))){
Toast.makeText(con.getApplicationContext(),“图像已在缓存中”,Toast.LENGTH\u SHORT.show();
返回ImageCache.showImage(Uri.parse(Image.getLastPathSegment());
}否则{
新建异步任务(){
受保护的布尔doInBackground(Void…params){
下载(con.getApplicationContext(),Image);
返回null;
}
}.execute();
Toast.makeText(con.getApplicationContext(),“图像已下载”,Toast.LENGTH\u SHORT.show();
}
而(ImageCache.spaceInMB(ImageCache.filePathFolder)>50){
deleteLastModified(ImageCache.filePathFolder,con.getApplicationContext());
}
//不起作用
//字符串s=testChecksum(新文件(filepathfolder,(Uri.parse(Image.getLastPathSegment()).trim())
返回null;
}else Toast.makeText(con.getApplicationContext(),“请插入有效的Url地址”,Toast.LENGTH_SHORT.show();
返回null;
}
}
静态字符串testChecksum(文件输出)抛出IOException、NoSuchAlgorithmException
{
MessageDigest md5=MessageDigest.getInstance(“md5”);
FileInputStream fis=新的FileInputStream(outputF);
字节[]数据=新字节[1024];
int read=0;
while((读取=fis.read(数据))!=-1){
md5.更新(数据,0,读取);
};
byte[]hashBytes=md5.digest();
StringBuffer sb=新的StringBuffer();
for(int i=0;i
I修复了格式设置(请参见插入代码的“{}”按钮)。从提供的代码示例中,没有对变量filepathfolder
的引用。是否可以将代码发布到初始化此变量的位置?