android 扫描手机内存和SD卡,获取手机的视频、音频文件。把获取不到的文件扫描出来

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cwg19910909/article/details/50268443
 网上查了下android手机扫描内存或者SD卡里面的音频或者视频文件,进行显示。但是有些视频或者音频扫描不出来。有其中一个原因是当你添加了一个视频或者音频文件后,手机自身没有重新进行扫描。只有手机重启之后,手机自身才会进行扫描,才能使用传统的扫描方法扫描出视频、音频文件来。
 所以可以自行添加一个扫描手机内存的方法,在扫描完之后,再使用传统的视频、音频文件获取方式获取来获取。

以下两个方法是用来启动扫描的广播使用的:

    //扫描SD卡的文件
    public void SendReceive_Scand_outside_Music(){
        IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        // intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        intentfilter.addDataScheme("file");
        ScanSdOutsideReceive scanSdReceiver = new ScanSdOutsideReceive();
        MyApp.getInstance().registerReceiver(scanSdReceiver, intentfilter);
        mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        //        MyApp.getInstance().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
//       Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));

    }

    //扫描内存的文件
    public void SendReceive_Scand_inside_Music(){
        IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        // intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        intentfilter.addDataScheme("file");
        ScanSdInsideReceive scanSdReceiver = new ScanSdInsideReceive();
        MyApp.getInstance().registerReceiver(scanSdReceiver, intentfilter);
        mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getDataDirectory())));
    }  

以下的办法就是扫描文件的广播:

public class ScanSdOutsideReceive extends BroadcastReceiver{


    //private AlertDialog.Builder  builder = null; 
    //private AlertDialog ad = null; 
//    private int count1; 
//    private int count2; 
//    private int count; 
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if(Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)){
//            Cursor c2 = context.getContentResolver() 
//            .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
//                    new String[]{MediaStore.Audio.Media.TITLE, 
//                    MediaStore.Audio.Media.DURATION, 
//                    MediaStore.Audio.Media.ARTIST, 
//                    MediaStore.Audio.Media._ID, 
//                    MediaStore.Audio.Media.DISPLAY_NAME }, 
//                    null, null, null);
        }
    }
}
public class ScanSdInsideReceive extends BroadcastReceiver{


    //private AlertDialog.Builder  builder = null; 
    //private AlertDialog ad = null; 
//    private int count1; 
//    private int count2; 
//    private int count; 
    @Override 
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction(); 
 if(Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)){ 
//            Cursor c2 = context.getContentResolver() 
//            .query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
//                    new String[]{MediaStore.Audio.Media.TITLE, 
//                    MediaStore.Audio.Media.DURATION, 
//                    MediaStore.Audio.Media.ARTIST, 
//                    MediaStore.Audio.Media._ID, 
//                    MediaStore.Audio.Media.DISPLAY_NAME }, 
//                    null, null, null); 
        }    
    } 
}

执行完扫描的广播之后。再调用获取手机的视频、音频的文件方法,也就是我上面所说的传统的获取方式:

获取视频:

            ContentResolver contentResolver = mContext.getContentResolver();
            Cursor cursor = contentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);

//          Cursor cursor = mContext.getContentResolver().query(
//                  MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
//                  null, null);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    int id = cursor.getInt(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media._ID));
                    String title = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
                    String album = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));
                    String artist = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
                    String displayName = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
                    String mimeType = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
                    String path = cursor
                            .getString(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                    long duration = cursor
                            .getInt(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
                    long size = cursor
                            .getLong(cursor
                                    .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));


                        }
                    }

                }
                cursor.close();
            }

获取音频:

        // ContentProvider(内容提供者) 和 ContentResolver(内容解析器),用于管理和发布和应用程序相关的持久性数据
        ContentResolver resolver = mContext.getContentResolver();
        //  String[] projection = new String[]{MediaStore.Video.Media.TITLE};
        Cursor cursor = resolver.query(
                MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
        if (null == cursor) {
            return null;
        }
        cursor.moveToFirst();
        if (cursor.moveToFirst()) {
            do {
                RingItem m = new RingItem();
//自己建的类,用于存放查询到的音乐信息
//播放音乐是用的是创建MediaPlayer实例,为其传递音乐文件的路径
                long id = 0;
                String title = null;
                String arrtist = null ;
                String album=null;
                long size=0;
                long duration=0;
                String url =null;
                String name=null;
                if(cursor.getColumnIndex(MediaStore.Audio.Media._ID)!=-1)
                    id = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media._ID));        //音乐id
                if(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)!=-1)
                    title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)); // 标题
                if(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)!=-1)
                    arrtist = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.ARTIST)); // 歌手
                if ("<unknown>".equals(arrtist)) {
                    arrtist = MyApp.getInstance().getResources().getString(R.string.unknow_artist);
                }
                if(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)!=-1)
                    album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)); // 专辑图片
                if(cursor.getColumnIndex(MediaStore.Audio.Media.SIZE)!=-1)
                    size = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.SIZE)); // 大小
                if(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION)!=-1)
                    duration = cursor.getLong(cursor .getColumnIndex(MediaStore.Audio.Media.DURATION)); // 时长
                if(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)!=-1)
                    url = cursor.getString(cursor .getColumnIndex(MediaStore.Audio.Media.DATA)); // 音乐文件的路径
                if(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)!=-1)
                    name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));// 音乐文件名
                if(url!=null&&duration>0){
                    File file = new File(url);
                    if (file.exists()) {

//此处添加music,音乐信息,到列表

                    }
                }
            } while (cursor.moveToNext());
        }
        if (cursor != null) {
//结果集移动到最后一列,再下移为空;释放资源
            cursor.close();
        }
展开阅读全文

没有更多推荐了,返回首页