![](/img/trans.png)
[英]Android - how to only show(or able to select) files with a custom extension when calling “file selector” using Intent.ACTION_GET_CONTENT
[英]Android - Limit Intent.ACTION_GET_CONTENT to a specific file extension
在我的應用中,我需要從用戶庫中獲取音頻文件。
為此,我稱之為“意圖”:
Intent selectIntent = new Intent(Intent.ACTION_GET_CONTENT);
selectIntent.setType("audio/*");
startActivityForResult(selectIntent, SONG_REQUEST_CODE);
通過將“ audio / *”作為setType()方法的參數,我可以從其擴展名中獲取每個音頻文件。
我需要做的就是過濾它,使其只能選擇.mp3文件
我試圖通過將“ .mp3”,“ audio / .mp3”作為setType()
方法的參數來實現,但是它們都setType()
。
步驟#1:查找所需文件類型的MIME類型。 如果是MP3,則為audio/mpeg
。
步驟#2:將該MIME類型傳遞給setType()
,而不是audio/*
。
請注意, ACTION_GET_CONTENT
與文件擴展名無關,部分原因是ACTION_GET_CONTENT
與文件無關。
我正在使用以下代碼
Intent mp3Intent;
mp3Intent= new Intent();
mp3Intent.setAction(Intent.ACTION_GET_CONTENT);
mp3Intent.setType("audio/mpeg");
startActivityForResult(Intent.createChooser(mp3Intent, getString(R.string.audio_file_title)), REQ_CODE_PICK_SOUNDFILE);
並使用以下代碼在onActivityResult中獲取uri- :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQ_CODE_PICK_SOUNDFILE && resultCode == Activity.RESULT_OK){
if ((data != null) && (data.getData() != null)){
Uri mp3FileUri = data.getData();
// Now you can use that Uri by passing to your MediaPlayer or can use ContentResolver and OutputInputStream() to read MP3 encode byte
}
}
}
有關更多擴展名和MIME類型,您可以在這里訪問
我認為這可能對您有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.