Android 掃描音樂檔案、兩種方式擷取檔案最新修改時間

來源:互聯網
上載者:User

標籤: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();@Overrideprotected 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;@Overridepublic void run() {while (cursor.moveToNext()) {//歌曲的名稱 :MediaStore.Audio.Media.TITLEString tilte = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));  //歌曲檔案的路徑 :MediaStore.Audio.Media.DATAString url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));  //方法1:歌曲檔案的修改時間 (注意:精確到秒,所以還要*1000):MediaStore.Audio.Media.DATE_MODIFIEDlong 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();}@Overridepublic 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;}}


Android 掃描音樂檔案、兩種方式擷取檔案最新修改時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.