你好,你可以看到下面我试图制作一个(android)应用程序,检查文件的md5哈希 此代码仅适用于小文件 有人能帮助我吗?
final TextView informations = (TextView) findViewById(R.id.Informations);
final EditText input = (EditText) findViewById(R.id.ToCrack);
String filepath = data.getDataString();
String rawtext;
String hash;
StringBuilder text = new StringBuilder();
filepath = filepath.split("//")[1];
File file = new File(filepath);
Toast.makeText(getApplicationContext(),"Loading: "+filepath,Toast.LENGTH_LONG).show();
FileInputStream fis = null;
BufferedInputStream bis = null;
DataInputStream dis = null;
try{
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
dis = new DataInputStream(bis);
while (dis.available() != 0){
text.append(dis.readLine()+"\n");
}
}
catch (IOException e){
e.printStackTrace();
}
finally {
try{
fis.close();
bis.close();
dis.close();
}
catch (IOException ex){
ex.printStackTrace();
}
}
rawtext = text.toString().substring(0, text.length()-1);
hash = new MD5(rawtext).hexdigest();
if (hash.equals(input.getText().toString())){
informations.setText("Hash correspond with the file!");
}
else{
informations.setText("File hash= "+hash+"\nHashes does not correspond :(");
}
Toast.makeText(getApplicationContext(),"Copied file hash to clipboard.",Toast.LENGTH_LONG).show();
答案 0 :(得分:2)
Android等移动设备环境对应用程序可以使用的内存量有限制。因此,将大文件读取到代码中的内存数据存储(使用StringBuffer
)将导致OutOfMemory错误。
看看this question,了解如何克服这个问题。