下面是我以前的做法
private void findLocalAudio(Intent data) {
Uri uri = data.getData();
String[] projection = {MediaStore.Audio.Media.DATA};
Cursor myCursor = this.getContentResolver().query(uri, projection, null, null, null);
int chooseIndex;
if (myCursor != null) {
myCursor.moveToFirst();
chooseIndex = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
String filePath = myCursor.getString(chooseIndex);
File choosePath = new File(filePath);
chooseLocalAudio(choosePath);
myCursor.close();
}
}
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 && cursor.moveToFirst()) {
do {
String filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
String localAudioPath = new File(filePath).toString();
if (choosePath.equals(localAudioPath)) {
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));
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);
mControllerView.setVisibility(View.VISIBLE);
mControllerView.setMediaUrl(localAudioPath);
OfflineCreateActivity.startOfflineCreateActivity(RecordingActivity.this, attachBean);
}
} while (cursor.moveToNext());
cursor.close();
}
}
}
专为Android4.4设计的从Uri获取文件绝对路径,以前的方法已不好使:
import android.annotation.SuppressLint;
import android.content.ContentUris;