1

我目前正在创建一个程序,它递归地搜索安卓手机上视频的 SD 卡,然后为每个视频生成哈希。

一切正常,但是当我尝试创建一个progress bar时,progress bar 直到结束才更新

所以基本上进度条的进度似乎从 0 到 max,但我可以从日志输出中看出值正在发送到进度条以更新它,进度条只是没有更新。

我的代码如下:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        progress = (ProgressBar) findViewById(R.id.progressBar1);
        text = (TextView) findViewById(R.id.tv1);
        Button btnStart = (Button) findViewById(R.id.btnStart);

        btnStart.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                recursiveVideoFinder(new File("/sdcard"));
                String strDB3File = getFilesDir().getPath()
                        + "/VideoHashes.db3";
                sql3 = SQLiteDatabase.openDatabase(strDB3File, null,
                        SQLiteDatabase.CREATE_IF_NECESSARY);
                try {
                    String mysql = "CREATE TABLE videohashes (id INTEGER  NOT NULL PRIMARY KEY AUTOINCREMENT, path TEXT NOT NULL, hash TEXT NOT NULL, date TEXT NOT NULL, size INTEGER)";
                    sql3.execSQL(mysql);
                } catch (SQLiteException e) {
                    // TODO: handle exception
                }

                progress.setProgress(0);
                progress.setMax(myFiles.size());

                for (String path : myFiles) {
                    try {
                        String hash = getMD5Checksum(path);
                        ContentValues myInsertData = new ContentValues();
                        myInsertData.put("path", path);
                        myInsertData.put("hash", hash);
                        Date date = new Date();
                        myInsertData.put("date", dateFormat.format(date));
                        myInsertData.put("size", getFileSize(path));
                        sql3.insert("videohashes", null, myInsertData);
                        currVid++;
                        updateProgress();
                        Log.i("Progress", "CurrVid:" + currVid + "  Max:"
                                + progress.getMax());
                        text.append(currVid + " ");

                    } catch (Exception e) {
                        Log.i("File", "File not Found");
                    }
                }

                Cursor cur = sql3.query("videohashes", null, null, null, null,
                        null, null);
                cur.moveToFirst();
                while (!cur.isAfterLast()) {
                    /*
                     * ((TextView) findViewById(R.id.tv1)).append("\n" +
                     * cur.getString(1) + "\n" + cur.getString(2) + "\n" +
                     * cur.getString(3) + "\n" + cur.getString(4) + "\n");
                     */
                    cur.moveToNext();
                }
                cur.close();
            }
        });

    }

    public long getFileSize(String path) {
        File file = new File(path);
        return file.length();

    }

    private void recursiveVideoFinder(File _dir) {

        File[] files = _dir.listFiles();
        if (files == null) {
            return;
        }

        for (File currentFile : files) {
            if (currentFile.isDirectory()) {
                    recursiveVideoFinder(currentFile);
                continue;
            } else {
                for (String aEnd : getResources().getStringArray(
                        R.array.VideoExtensions)) {
                    if (currentFile.getName().endsWith(aEnd)) {

                        Log.d("PS:", currentFile.getPath().toString());

                        String videoFileName = currentFile.getPath().toString();
                        myFiles.add(videoFileName);

                    }
                }

            }
        }
    }

    // Code based off this example:
    // http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java
    public static byte[] createCheckSum(String filename) throws Exception {
        InputStream fis = new FileInputStream(filename);

        byte[] buffer = new byte[1024];
        MessageDigest complete = MessageDigest.getInstance("MD5");
        int numRead;

        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);

        fis.close();
        // Return MD5 Hash
        return complete.digest();
    }

    public String getMD5Checksum(String filename) throws Exception {
        byte[] b = createCheckSum(filename);
        String result = "";

        for (int i = 0; i < b.length; i++) {
            result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return result;
    }

    public void updateProgress() {
        progress.setProgress(currVid);
        progress.invalidate();
        Log.i("Updating", "Updating Progress Bar");
    }
}
4

2 回答 2

2

为什么要把所有的处理代码都保留在 Onclick() 中。这不是一个好方法。创建一个AsyncTask并使用 publishProgress 来更新你的进度条。

于 2012-04-13T04:24:57.830 回答
0

通过覆盖 onResume() 来完成更新任务。在那里启动一个新的计时器线程并继续调用您的函数。当您单击返回时,也可以通过说 timer.cancel() 来完成该线程。因此,每当您进行该活动时,只有计时器才会启动并在回击时退出。检查此以获取更多信息

于 2012-04-13T05:17:29.477 回答