【发布时间】:2013-05-18 18:50:40
【问题描述】:
您好,正如您在下面看到的,我正在尝试制作一个(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();
【问题讨论】:
-
您是否尝试过使用 AsyncTask 在后台线程中读取文件? developer.android.com/reference/android/os/AsyncTask.html
-
我没试过,但是这段代码直接让应用程序崩溃,所以我猜它不会工作
-
编辑您的问题并包含堆栈跟踪。
-
删除
available()测试。您的代码将与while ((line = in.readLine()) != null)相同。available()的正确用法很少,这不是其中之一。
标签: java android io md5 java-io