2013-07-30 32 views
14

Nhiệm vụ hiện tại của tôi là chọn một tập tin âm thanh URI từ sdcard. Các bạn có thể cầu xin tôi trong những câu hỏi này không. Sự giúp đỡ của bạn sẽ rất được đánh giá cao. Cảm ơn bạnChọn một tập tin âm thanh android

EDIT

tôi phải nhận được một tập tin âm thanh từ SD Card và chơi nó. Tôi nghĩ rằng tôi có thể thực hiện nhiệm vụ này bằng cách nhận được URI của một tập tin âm thanh. Vì vậy, để chọn tệp âm thanh tôi đang sử dụng theo khối mã sau:

Intent intent = new Intent(); 
intent.setType("audio/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"Select Audio "), reqCode); 

Bây giờ tôi có thể duyệt tệp âm thanh và tôi có thể chọn tệp đó.

CÂU HỎI: Cách đọc URI của tệp được chọn trong onActivityResult của tôi?

Cảm ơn bạn

+0

Tôi đang làm việc trên đó 2-3 giờ qua. Nhưng tôi không biết cuốc để lấy URI. Tôi có thể mở Bộ chọn âm thanh, sau khi chọn bất kỳ âm thanh nào để nhận URI của nó? –

+1

@SanjayJoshi kiểm tra câu trả lời của tôi. – Bishan

Trả lời

31

Bạn có thể đặt các mã bên dưới vào dự án khi bạn muốn chọn âm thanh.

Intent intent_upload = new Intent(); 
intent_upload.setType("audio/*"); 
intent_upload.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(intent_upload,1); 

Và ghi đè onActivityResult trong Hoạt động tương tự, như sau

@Override 
protected void onActivityResult(int requestCode,int resultCode,Intent data){ 

    if(requestCode == 1){ 

    if(resultCode == RESULT_OK){ 

     //the selected audio. 
     Uri uri = data.getData(); 
    } 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 
+0

theo tôi một cách dễ dàng hơn là: Mục đích ý định = Mục đích mới (Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); startActivityForResult (intent, 1); thay vì sử dụng intent_upload.setType ("audio/*"); và phần còn lại của phần là tốt. cảm ơn – enam

+0

tính năng này không hoạt động đối với các tệp .opus – Borja

0
final Uri uri=Uri.parse(Environment.getExternalStorageDirectory()+"/Audio/abc.mp3"); 

Thay /Audio/abc.mp3 với your path of mp3 file on sdcard.

Đừng quên kiểm tra xem bộ nhớ ngoài có được lắp hay không. Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

0
first of all open gallery through intent - 
    public void openGalleryForAudio() { 
     Intent videoIntent = new Intent(
       Intent.ACTION_PICK, 
       android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(Intent.createChooser(videoIntent, "Select Audio"), AUDIO_REQUEST); 
} 


Then onActivityResult you should catch data - 
if (requestCode == AUDIO_REQUEST && null != data) { 
       if (requestCode == AUDIO_REQUEST) { 

        Uri uri = data.getData(); 
        try { 
         String uriString = uri.toString(); 
         File myFile = new File(uriString); 
         // String path = myFile.getAbsolutePath(); 
         String displayName = null; 
         String path2 = getAudioPath(uri); 
         File f = new File(path2); 
         long fileSizeInBytes = f.length(); 
         long fileSizeInKB = fileSizeInBytes/1024; 
         long fileSizeInMB = fileSizeInKB/1024; 
         if (fileSizeInMB > 8) { 
          customAlterDialog("Can't Upload ", "sorry file size is large"); 
         } else { 
          profilePicUrl = path2; 
          isPicSelect = true; 
         } 
        } catch (Exception e) { 
         //handle exception 
         Toast.makeText(GroupDetailsActivity.this, "Unable to process,try again", Toast.LENGTH_SHORT).show(); 
        } 
        // String path1 = uri.getPath(); 

       } 
      } 

This function is use for absolute path of audio file 
private String getAudioPath(Uri uri) { 
     String[] data = {MediaStore.Audio.Media.DATA}; 
     CursorLoader loader = new CursorLoader(getApplicationContext(), uri, data, null, null, null); 
     Cursor cursor = loader.loadInBackground(); 
     int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); 
     cursor.moveToFirst(); 
     return cursor.getString(column_index); 
    } 
+1

Bạn có thể muốn thêm một số thông tin, không chỉ là một khối mã. – DGarvanski

Các vấn đề liên quan