我正在尝试这个,公然从
https://www.programcreek.com/java-api-examples/index.php?class=java.util.zip.CheckedInputStream&method=read
:
String apkPath = App.getContext().getPackageCodePath();
Log.i("Checksum", "getting checksum for " + apkPath);
Long chksum = null;
Log.i("Checksum", "Size: " + new File(apkPath).length());
Log.i("Checksum", "Date: " + new Date(new File(apkPath).lastModified()));
try {
// Open the file and build a CRC32 checksum.
FileInputStream fis = new FileInputStream(new File(apkPath));
CRC32 chk = new CRC32();
CheckedInputStream cis = new CheckedInputStream(fis, chk);
byte[] buff = new byte[80];
while (cis.read(buff) >= 0) ;
chksum = chk.getValue();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("Checksum", "Checksum is " + chksum);
运行良好,输出良好:
09-27 15:17:35.775 17288-17288/com.test.checksum.checksum I/Checksum: getting checksum for /data/app/com.test.checksum.checksum-2/base.apk
09-27 15:17:35.775 17288-17288/com.test.checksum.checksum I/Checksum: Size: 3245610
09-27 15:17:35.775 17288-17288/com.test.checksum.checksum I/Checksum: Date: Wed Sep 27 15:17:03 BST 2017
09-27 15:17:37.075 17288-17288/com.test.checksum.checksum I/Checksum: And Checksum is 0DAD53E0
问题是,更改代码似乎没有任何区别-因此,以下代码:
String apkPath = App.getContext().getPackageCodePath();
Log.i("Checksum", "getting checksum for " + apkPath);
Long chksum = null;
Log.i("Checksum", "Size: " + new File(apkPath).length());
Log.i("Checksum", "Date: " + new Date(new File(apkPath).lastModified()));
//do something differently
Log.i("Checksum", "And now do something else!");
Toast toast = Toast.makeText(getApplicationContext(), "I'm doing something else!", Toast.LENGTH_SHORT);
toast.show();
try {
// Open the file and build a CRC32 checksum.
FileInputStream fis = new FileInputStream(new File(apkPath));
CRC32 chk = new CRC32();
CheckedInputStream cis = new CheckedInputStream(fis, chk);
byte[] buff = new byte[80];
while (cis.read(buff) >= 0) ;
chksum = chk.getValue();
} catch (Exception e) {
e.printStackTrace();
}
Log.i("Checksum", "Checksum is " + String.format("%08X", chksum));
生成此输出:
09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: getting checksum for /data/app/com.test.checksum.checksum-2/base.apk
09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: Size: 3245610
09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: Date: Wed Sep 27 15:45:30 BST 2017
09-27 15:45:51.085 29517-29517/com.test.checksum.checksum I/Checksum: And now do something else!
09-27 15:45:52.335 29517-29517/com.test.checksum.checksum I/Checksum: Checksum is 0DAD53E0
File().list()
谢谢