Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 掃描音樂文件、兩種方式獲取文件最新修改時間

Android 掃描音樂文件、兩種方式獲取文件最新修改時間

編輯:關於Android編程



package com.example.demo_mediascanner;

import java.io.File;
import java.util.Calendar;
import java.util.Date;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
/**
 * 掃描音樂 --scanner music
 * @author alan
 *
 */
public class MainActivity extends Activity {
	
	private Cursor cursor ;
	Calendar cal = Calendar.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	    IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);  
	    intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);    
	    intentfilter.addDataScheme("file");    
	    MediaScannerReceiver receiver = new MediaScannerReceiver();  
	    registerReceiver(receiver, intentfilter);  

	    
	    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
	    		
                + Environment.getExternalStorageDirectory())));
	    
	}

	public void scanFileAsync(Context ctx, String filePath) {
		Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		scanIntent.setData(Uri.fromFile(new File(filePath)));
		ctx.sendBroadcast(scanIntent);
	}

	
	public void scanner(View view){
		
		
		cursor =   getContentResolver().
		query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,null, null, 
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		
		Thread thread = new Thread(){
			int n;
			@Override
			public void run() {	
				
				while (cursor.moveToNext()) {
					//歌曲的名稱 :MediaStore.Audio.Media.TITLE
					String tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));  
					//歌曲文件的路徑 :MediaStore.Audio.Media.DATA
					String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));  
					//方法1:歌曲文件的修改時間 (注意:精確到秒,所以還要*1000):MediaStore.Audio.Media.DATE_MODIFIED
					long updateTime= cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATE_MODIFIED)); 
					updateTime = updateTime*1000;
					//方法2:獲取最新修改時間
					File file = new File(url);
					long time = file.lastModified();
					//格式化時間,獲取年,月,日
					String[] times =  getTimeInfo(updateTime);
					
					
//					Log.i("TAG", "tilte:"+tilte);
					Log.i("TAG", "url:"+url);
					Log.i("TAG", "times:"+times[0]+times[1]+times[2]);
					n++;
				}
				Log.i("TAG", "n====>"+n);
				Log.i("TAG", "getCount====>"+cursor.getCount());
			}
		};
		thread.start();
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	/**
	 * 獲取時間(年月日)
	 * @return
	 */
	public String[] getTimeInfo(long time){
		String[] times  = new String[3];
		Date date = new Date(time);
		cal.setTime(date);
		 times[0] = cal.get(Calendar.YEAR)+"";
         times[1]= (cal.get(Calendar.MONTH)+1)+"";//calendar月份從0-11
         times[2]= cal.get(Calendar.DAY_OF_MONTH)+"";
		
		return times;
	}
	

}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved