关于Android获取本地音频的坑

下面是我以前的做法

    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;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;

public class GetPathFromUri4kitkat {

    @SuppressLint("NewApi")
    public static String getPath(final Context context, final Uri uri) {

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{split[1]};

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context       The context.
     * @param uri           The Uri to query.
     * @param selection     (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {column};

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
                    null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }
}

应该是:

private void findLocalAudio(Intent data) {
    Uri uri = data.getData();
    String choosePath = GetPathFromUri4kitkat.getPath(RecordingActivity.this, uri);
    chooseLocalAudio(choosePath);
    }
}

然而然而!!!! 还有一个问题:
项目中可能有这样的需求:下载或导入、导出的图片、音乐等媒体文件,需要马上能在图库或本地视屏播放器中显示出来,或者要能在媒体数据库中查询到媒体文件的相关信息,这时我们就得主动通知系统扫描新的媒体文件了. 这是就需要一个辅助扫描类来帮个忙.
这个扫描的工具类来自 :John-Chen

public class MediaScanner {

    private volatile static MediaScanner instance;

    private MediaScanner(){
    }

    public static MediaScanner getInstace(){
        synchronized (MediaScanner.class){
            if(instance == null){
                instance = new MediaScanner();
            }
        }
        return instance;
    }


    /**
     * 扫描一个媒体文件
     * @param filePath 要扫描的媒体文件
     */
    public void scanFile(Context context, ScanFile filePath) {
        List filePaths = new ArrayList(1);
        filePaths.add(filePath);
        scanFiles(context, filePaths);
    }

    /**
     * 扫描多个媒体文件
     * @param filePaths 要扫描的文件列表
     */
    public void scanFiles(Context context, List filePaths){
        SannerClient client = new SannerClient(context, filePaths);
        client.connectAndScan();
    }


    /**
     * 媒体文件扫描对象构造器
     */
    public static class ScanFile{

        /**
         * 要扫描的媒体文件路劲或包含媒体文件的文件夹路径
         */
        public String filePaths;

        /**
         * 要扫描的媒体文件类型 eg: audio/mp3  media/*  application/ogg
         *             image/jpeg  image/png  video/mpeg   video/3gpp
         *             ......
         */
        public String mineType;

        public ScanFile(String filePaths, String mineType) {
            this.filePaths = filePaths;
            this.mineType = mineType;
        }
    }

    public class SannerClient implements
            MediaScannerConnection.MediaScannerConnectionClient {

        /**
         * 要扫描的文件或文件夹
         */
        private List scanFiles = null;

        /**
         * 实际要扫描的单个文件集合
         */
        private List filePaths = null;

        private MediaScannerConnection mediaScanConn;

        public SannerClient(Context context, List scanFiles) {
            this.scanFiles = scanFiles;
            mediaScanConn = new MediaScannerConnection(context, this);
        }

        /**
         * 连接MediaScanner并开始扫描
         */
        public void connectAndScan(){
            if(scanFiles != null && !scanFiles.isEmpty()){
                this.filePaths = new ArrayList();

                //遍历取得单个文件集合
                for(ScanFile sf : scanFiles){
                    findFile(sf);
                }

                mediaScanConn.connect();
            }
        }

        private void findFile(ScanFile file){
            File f = new File(file.filePaths);
            if(f.isFile()){
                filePaths.add(file);

            }else{
                File[] fs = f.listFiles();
                if(fs != null && fs.length > 0){
                    for(File cf : fs){
                        findFile(new ScanFile(cf.getAbsolutePath(), file.mineType));
                    }
                }
            }
        }

        private void scanNext(){
            if(filePaths != null && !filePaths.isEmpty()){
                ScanFile sf = filePaths.remove(filePaths.size() - 1);
                mediaScanConn.scanFile(sf.filePaths, sf.mineType);

            }else{
                mediaScanConn.disconnect();
            }
        }

        @Override
        public void onMediaScannerConnected() {
            scanNext();
        }

        @Override
        public void onScanCompleted(String path, Uri uri) {
            // TODO Auto-generated method stub
            scanNext();

            //媒体扫描完成可以配合EventBus等消息通讯工具发出通知,也可接收Intent.ACTION_MEDIA_SCANNER_FINISHED的广播
            //EventBus.getDefault().post(new EventMediaScanCompleted(path));
        }
    }
}

PS:
MediaScanner.getInstace().scanFile(this, new MediaScanner.ScanFile(Environment.getExternalStorageDirectory().getAbsolutePath() + “/tencent”, “audio/*”));

依然存在扫描时机不合理的情况.
关于Android获取本地音频的坑2 → http://blog.csdn.net/heartyhu/article/details/53536703

你可能感兴趣的:(Android)