最近在學(xué)習(xí)安卓開發(fā),有一些JAVA基礎(chǔ),想做一個簡單的視頻播放器,現(xiàn)在卡到了獲取視頻文件路徑這一點(diǎn)上,現(xiàn)在已經(jīng)會用Mediaplay,video等控件,但是只能每一次在AS中輸入程序路徑,想能夠在程序上去獲取本地所有視頻路徑,然后可以選擇播放,可是網(wǎng)上查了很多,很多代碼也運(yùn)行不起也看的不是很懂,所以希望大家能給一個思路,我好在去學(xué)習(xí)。這個查了快一點(diǎn)了,也沒有解決!謝謝大家
業(yè)精于勤,荒于嬉;行成于思,毀于隨。
Two ways
1. It’s simple. Call the file selector in the system to help you find the file you need and return the path to you. The code is small and easy. It’s as simple as this:
private void pickFile() {
Intent intent = new Intent();
//意圖類型過濾,指定視頻類型文件
intent.setType("video/*");
//意圖動作,選取內(nèi)容
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 0x01);
}
.
.
.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0x01 && resultCode == RESULT_OK) {
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
//獲取文件路徑
String path = cursor.getString(1);
}
}
2. Implement your own file manager and filter the file types you need by scanning the files on the system memory. It is a bit more complicated. It involves ContentResolver,Uri,Cursor,MediaStore,MimeType,Intent
and other major knowledge points. It is definitely complicated and troublesome. Don't be afraid of trouble when you make something good, just go and eat it.
The first way lets you know what it is, and the second way lets you know why.
Write the word "wang" correctly, not "forget".
The second method is to give you these references and follow them to implement it yourself. That’s almost it.
First of all, let me solve your doubts about how to obtain all local video paths. When the Android system stores video, audio, pictures and other resources, it will automatically store its related information in the database. The information includes name, size, storage path, etc. If we play a certain video file, get its storage path from the database, and then get the video itself through the path. Then you need to do the following things:
1. Understand the name and fields of the database that stores Android video information
2. Understand the method of obtaining Android to obtain data in the database. Android has been packaged
Look at the code for scanning all Video information. I searched casually, so I'm not sure it's correct. I'll explain it to you.
private void scanVideoUri(){
//通過ContentResolver從數(shù)據(jù)庫獲取信息,Curse是獲取的結(jié)果
Cursor cursor = mContext.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
null, null, null, null);
int totalCount =cursor.getCount();//計(jì)算所有結(jié)果的條數(shù)
cursor.moveToFirst();//此句一定要有
//遍歷所有的Video信息
for( int i = 0;i < totalCount;i++){
String data = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
String data1 = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.TITLE));
String type = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE));
int id = cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID));
Log.e(TAG, data+title+type);
cursor.moveToNext();//訪問下一個
}
}
It’s normal if you don’t understand the code. Break the target down and then splice it together to get the final result.
Read the system database, and all the videos that can be seen in your mobile phone video list can be directly searched. For specific implementation, you can ask Du Niang~