Java Android用户界面未更新
我目前正在创建一个程序,在android手机上递归搜索视频的SD卡,然后为每个视频生成哈希 一切正常,但当我尝试创建一个Java Android用户界面未更新,java,android,Java,Android,我目前正在创建一个程序,在android手机上递归搜索视频的SD卡,然后为每个视频生成哈希 一切正常,但当我尝试创建一个进度条时,进度条直到最后才更新 所以本质上进度条的进度看起来是从0到最大,但我可以从日志输出中看出,正在将值发送到进度条以更新它,进度条只是没有升级 我的代码如下: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceSta
进度条时,进度条直到最后才更新
所以本质上进度条的进度看起来是从0到最大,但我可以从日志输出中看出,正在将值发送到进度条以更新它,进度条只是没有升级
我的代码如下:
@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");
}
}
@覆盖
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
进度=(ProgressBar)findViewById(R.id.progressBar1);
text=(TextView)findViewById(R.id.tv1);
按钮btnStart=(按钮)findViewById(R.id.btnStart);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
递归视频查找器(新文件(“/SD卡”);
字符串strDB3File=getFilesDir().getPath()
+“/VideoHashes.db3”;
sql3=SQLiteDatabase.openDatabase(strDB3File,null,
SQLiteDatabase.CREATE_(如果需要);
试一试{
String mysql=“创建表videohashes(id整数不为NULL主键自动递增、路径文本不为NULL、哈希文本不为NULL、日期文本不为NULL、大小整数)”;
execSQL(mysql);
}catch(sqlitee异常){
//TODO:处理异常
}
进度。设置进度(0);
progress.setMax(myFiles.size());
用于(字符串路径:myFiles){
试一试{
String hash=getMD5Checksum(路径);
ContentValues myInsertData=新ContentValues();
myInsertData.put(“path”,path);
put(“hash”,hash);
日期=新日期();
myInsertData.put(“date”,dateFormat.format(date));
put(“size”,getFileSize(path));
sql3.insert(“videohashes”,null,myInsertData);
currVid++;
updateProgress();
Log.i(“进度”,“CurrVid:+CurrVid+”最大值:”
+progress.getMax());
text.append(currVid+“”);
}捕获(例外e){
Log.i(“文件”,“未找到文件”);
}
}
Cursor cur=sql3.query(“视频哈希”,null,null,null,
空,空);
cur.moveToFirst();
而(!cur.isAfterLast()){
/*
*((TextView)findViewById(R.id.tv1)).append(“\n”+
*当前getString(1)+“\n”+当前getString(2)+“\n”+
*当前getString(3)+“\n”+当前getString(4)+“\n”);
*/
cur.moveToNext();
}
cur.close();
}
});
}
公共长getFileSize(字符串路径){
文件=新文件(路径);
返回file.length();
}
私有void recursiveVideoFinder(文件目录){
File[]files=_dir.listFiles();
if(files==null){
返回;
}
用于(文件当前文件:文件){
if(currentFile.isDirectory()){
递归视频查找器(当前文件);
继续;
}否则{
对于(字符串aEnd:getResources().getStringArray(
R.array.VideoExtensions){
if(currentFile.getName().endsWith(aEnd)){
Log.d(“PS:,currentFile.getPath().toString());
字符串videoFileName=currentFile.getPath().toString();
添加(视频文件名);
}
}
}
}
}
//基于此示例的代码:
// http://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java
公共静态字节[]createCheckSum(字符串文件名)引发异常{
InputStream fis=新文件InputStream(文件名);
字节[]缓冲区=新字节[1024];
MessageDigest complete=MessageDigest.getInstance(“MD5”);
国际货币联盟;
做{
numRead=fis.read(缓冲区);
如果(numRead>0){
完成。更新(缓冲区,0,numRead);
}
}而(numRead!=-1);
fis.close();
//返回MD5散列
返回complete.digest();
}
公共字符串getMD5Checksum(字符串文件名)引发异常{
字节[]b=创建校验和(文件名);
字符串结果=”;
for(int i=0;i
为什么要将所有处理代码保留在Onclick()中。这不是一个好办法。创建并使用publishProgress更新进度栏。通过覆盖onResume()来放置更新任务。在那里启动一个新的计时器线程,并继续调用您的函数。单击“上一步”时,也可以通过说出timer.cancel()来完成该线程。因此,每当你进行该活动时,只有在回击时计时器才会启动和退出 这个问题不仅仅局限于进度条。我也无法更新textview的。它们被声明在这里包含的代码之上,但是我没有包含PAR。