关于 Android 获取本地音频的坑2

来源:互联网 发布:ug铣螺纹编程 编辑:程序博客网 时间:2024/05/16 15:26
private void findLocalAudio(Intent data) {        Uri uri = data.getData();        String filePath = GetPathFromUri4kitkat.getPath(RecordingActivity.this, uri);        MediaScannerConnection.scanFile(RecordingActivity.this, new String[]{filePath}, null, new ScanCompletedListener());    }    private void chooseLocalAudio(String choosePath) {        ContentResolver contentResolver = this.getContentResolver();        AttachBean attachBean = new AttachBean();        if (contentResolver != null) {            Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);            if (cursor != null) {                if (cursor.moveToFirst()) {                    do {                        String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));                        String localAudioPath = new File(filePath).toString();                        if (localAudioPath.equals(choosePath)) {                            String displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));                            long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));                            long size = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE));                            long dateAdded = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DATE_ADDED)) * 1000;                            String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));                            if (duration <= 0) {                                Toast.makeText(this, "音频损坏", Toast.LENGTH_SHORT).show();                            }                            attachBean.setAttachType(AttachColumns.Type.AUDIO);                            attachBean.setAddTime(dateAdded);                            attachBean.setCreateTime(String.valueOf(dateAdded));                            attachBean.setCloud(false);                            attachBean.setFileSize(String.valueOf(size));                            attachBean.setFilePath(localAudioPath);                            attachBean.setLocalDisplayName(Uri.decode(displayName));                            attachBean.setTotalTime(String.valueOf(duration));                            attachBean.setTitle(title);                            OfflineCreateActivity.startOfflineCreateActivity(RecordingActivity.this, attachBean);                        }                    } while (cursor.moveToNext());                }                cursor.close();            }        }    }
private class ScanCompletedListener implements MediaScannerConnection.OnScanCompletedListener {       @Override       public void onScanCompleted(String path, Uri uri) {           if (path != null) {               switch (path.substring(path.lastIndexOf("."))) {                   case ".mp3":                   case ".ogg":                   case ".wav":                       chooseLocalAudio(path);                       break;                   default:                       ToastUtils.show(RecordingActivity.this, "音频格式暂时只支持主流的 mp3 ogg wav");                       break;               }           }       }   }
0 0
原创粉丝点击