比较两个文件是否相同,获取文件MD5值, HandlerThread 使用

比较两个文件是否相同,获取文件MD5值

通过adb 或者串口 的 md5 指令获取某个文件的MD5 值

adb shell

cd storage/sdcard0/  
root@g0003ah:/storage/sdcard0 # md5 tsp.jar

5cbfb75d1db4121b4b9e3a00dde49a18  tsp.jar   // 此值即为这个文件的md5值

也可以比较两个文件的md5

root@g0003ah:/storage/sdcard0 # md5 tsp.jar  t1sp.jar                          
5cbfb75d1db4121b4b9e3a00dde49a18  tsp.jar
5cbfb75d1db4121b4b9e3a00dde49a18  t1sp.jar

可以看出这个两个jar是同一个jar


用串口也可以.打开串口的方法

cd  /dev

sudo minicom -D ttyUSB*

//输入密码

指令如上

root@g0003ah:/mnt/sdcard0 # md5 tsp.jar                                        
5cbfb75d1db4121b4b9e3a00dde49a18  tsp.jar

Application 中通过utils 获取文件的md5 值


随便写个demo

文件目录为:/mnt/sdcard0/md5.txt

MainActivity:

package com.tan.md5;

import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.tan.md5.utils.Utils;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getSimpleName();

    private TextView vFileMD5Text;

    private Button vFileMD5Btn;
    private String mFilePath = "/mnt/sdcard0/md5.txt";

    private static final HandlerThread sWorkerThread = new HandlerThread("FileMD5-thread");

    static {
        sWorkerThread.start();
    }

    /**
     * sWorker队列执行任务,post执行完做下一个post
     */
    protected static final Handler sWorker = new Handler(sWorkerThread.getLooper());


    private static final int MESSAGE_FILE_MD5 = 100;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MESSAGE_FILE_MD5:
                    vFileMD5Text.setText((String)msg.obj);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initListener();
    }

    private void initView() {
        vFileMD5Text = (TextView) findViewById(R.id.tv_file1_md5);
        vFileMD5Btn = (Button) findViewById(R.id.btn_File1_MD5);
    }

    private void initListener() {
        vFileMD5Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFileMD5();
            }
        });
    }

    private void getFileMD5() {
        sWorker.post(new Runnable() {
            @Override
            public void run() {
                String strMD5 = Utils.getMd5sum(mFilePath);
                Message msg = mHandler.obtainMessage();
                msg.what = MESSAGE_FILE_MD5;
                msg.obj = strMD5;
                mHandler.sendMessage(msg);
            }
        });
    }

}

Utils:

package com.tan.md5.utils;

import android.util.Log;

import java.io.FileInputStream;
import java.io.InputStream;
import java.security.MessageDigest;

/**
 * Created by pateo on 18-2-6.
 */

public class Utils {
    private static final String TAG = Utils.class.getSimpleName();

    public static String getMd5sum(String filename) {
        InputStream fis;
        byte[] buffer = new byte[1024];
        int numRead = 0;
        MessageDigest md5;
        try {
            fis = new FileInputStream(filename);
            md5 = MessageDigest.getInstance("MD5");
            while ((numRead = fis.read(buffer)) > 0) {
                md5.update(buffer, 0, numRead);
            }
            fis.close();
            return toHexString(md5.digest());
        } catch (Exception e) {
            return null;
        }
    }

    public static String toHexString(byte[] b) {
        Log.d(TAG , "--toHexString --b=" + b.length + "---b=" + b.toString() );
        char HEX_DIGITS[] = {
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
        };

        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >> 4]);
            sb.append(HEX_DIGITS[b[i] & 0x0f]);
        }
        Log.d(TAG , "--toHexString -sb=" + sb.toString() );
        return sb.toString();
    }


}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.tan.md5.MainActivity"
    android:padding="30dp"
    android:orientation="vertical">


    <TextView
        android:id="@+id/tv_file1_md5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/tv_file2_md5"
        android:layout_width="wrap_content"
        android:layout_marginTop="30px"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_File1_MD5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取MD5"/>

</LinearLayout>
AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tan.md5">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值