android 多媒体数据刷新和操作,以及错误排查

 

android中的所有文件的维护和管理都是通过系统的多媒体数据库,这对于提升速度和用户提验十分有用,应用的所有的文件尽量从数据库中获取,频繁的IO操作既会带来同步,异步错误,又会降低运行的速度。

对数据库文件操作,必然关系到数据的同步,同步是否成功,可以使用adb shell content query --uri content://media/external/video/media查询数据库确认,也可以使用adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard/命令行刷新数据库


adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard/photos/1588754522340video.mp4
adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_FILE -d file:///sdcard/photos/1588754522340video.mp4
adb shell am broadcast -a android.intent.action.MEDIA_SCANNER_SCAN_DIR -d file:///sdcard/photos/


adb shell content query --uri content://media/external/video/media | grep "/16/photos"

 同步有个坑,传输的文件路径是必须使用Environment.getExternalStorageDirectory().getAbsolutePath()获取的动态路径,而不是/sdcard/photos/1588754522340video.mp4,还有另一种同步方案是这个,MediaScannerConnection.scanFile,这个不区别路径,可以使用/sdcard/photos/1588754522340video.mp4,广播支持文件夹同步,各有千秋,根据场景择合适的用之。

public class SyncMedia {
    public static final String TAG = SyncMedia.class.getSimpleName();
    public static final String ACTION_MEDIA_SCANNER_SCAN_DIR = "android.intent.action.MEDIA_SCANNER_SCAN_DIR";

    public static void scanSdCard(Context context, String filePath){
        //path必须是这种Environment.getExternalStorageDirectory().getAbsolutePath()+"/photos/1588754522340video.mp4"
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
    }

    private static ScanSdReceiver scanSdReceiver = new ScanSdReceiver();
    public static void registerScanSdcard(){
        IntentFilter intentfilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_STARTED);
        intentfilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        intentfilter.addDataScheme("file");
        AppUtil.getContext().registerReceiver(scanSdReceiver, intentfilter);
    }
    public static void unregisterScanSdcard(){
        AppUtil.getContext().unregisterReceiver(scanSdReceiver);
    }

    public static class ScanSdReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            LogUtil.d(TAG, "action = "+action);
            if (Intent.ACTION_MEDIA_SCANNER_STARTED.equals(action)){
                LogUtil.d(TAG, "action = "+ "ACTION_MEDIA_SCANNER_STARTED");
            }else if(Intent.ACTION_MEDIA_SCANNER_FINISHED.equals(action)){
                LogUtil.d(TAG, "action = "+ "ACTION_MEDIA_SCANNER_STARTED");
            }
        }
    }

    @SuppressLint("NewApi")
    public static void mediaScan(final Activity context, File file) {
        MediaScannerConnection.scanFile(context,
                new String[] { file.getAbsolutePath() }, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(final String path, Uri uri) {
                        Log.v("MediaScanWork", "file " + path
                                + " was scanned seccessfully: " + uri);
                        context.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context, "onScanCompleted"+path, Toast.LENGTH_LONG).show();
                                //Toast.makeText(context, "onScanCompleted", Toast.LENGTH_LONG).show();
                            }
                        });

                    }
                });
    }
}

 

 

参考:

https://blog.csdn.net/feiyangbahu1/article/details/70238211

http://wossoneri.github.io/2016/10/15/[Android]Multimedia-database/

https://www.jianshu.com/p/90832a323221

Camera ISO、快门、光圈、曝光这几个概念

https://blog.csdn.net/armwind/article/details/52413177

你可能感兴趣的:(android应用开发)