Android adb 打开mediascanner
2016-03-28 12:27
381 查看
On my current project, we need to “pre-load” many tablets with a set of videos. We want to script the loading of the videos, so we started with adb. The videos should appear in Android’s gallery app so they can be viewed like any other video. Finally, we have
some folder structure for the videos that should also appear in the gallery.
Getting this working was a couple hours’ challenge. Copying the files on to the tablet was easy. We did that in bulk using adb’s push command:
This is not enough to make the video appear in the gallery. Rather than scanning or watching the filesystem, Android’s gallery reads a ContentProvider that lists the media on the device. The ContentProvider is populated by a MediaScannerService that looks for
media at particular times and saves information about any discovered music, videos or images.
We needed a way to trigger the MediaScanner. Naively, we tried restarting the tablet, but that didn’t get the videos in to the gallery. Next, we asked google. We found several general descriptions of the process, but not in enough detail to easily duplicate
it. Here is what we discovered after experimentation.
The basic form of the command is:
or you can run it within an dab shell on a device:
What should the <ACTION> and <DATA_URI> values be?
Action needs to be a recognized constant value from the Android documentation. I kept tripping myself up by using the constant name, rather than the value. Values are in the documentation for the constants:
The two that we found useful were android.intent.action.MEDIA_MOUNTED and android.intent.action.MEDIA_SCANNER_SCAN_FILE. As described in their respective documentation, MEDIA_MOUNTED takes a path to scan as its data_uri and MEDIA_SCANNER_SCAN_FILE takes a path
to a single file. For our use case, we ran the command:
We had originally tried to use /mnt/sdcard/ as the full path, because it didn’t seem that there would be any harm in scanning the whole card. However, because different system apps watch paths on the sdcard, trying to say the whole card was mounted causes a
permissions error.
Because of that error, we had to be specify /Movies/, which did not trigger the error. If we do need to load music or images, we will be able to pass those specific paths with the same intent.
(Note: I’ve been unable to reproduce the permissions error more recently, so there is another factor involved. One difference could be that the tablets reporting the error were running 4.0.3, but we have since moved to tablets running 4.1.1.)
some folder structure for the videos that should also appear in the gallery.
Getting this working was a couple hours’ challenge. Copying the files on to the tablet was easy. We did that in bulk using adb’s push command:
adb push Movies/ /mnt/sdcard/Movies/remote_video.mp4
This is not enough to make the video appear in the gallery. Rather than scanning or watching the filesystem, Android’s gallery reads a ContentProvider that lists the media on the device. The ContentProvider is populated by a MediaScannerService that looks for
media at particular times and saves information about any discovered music, videos or images.
We needed a way to trigger the MediaScanner. Naively, we tried restarting the tablet, but that didn’t get the videos in to the gallery. Next, we asked google. We found several general descriptions of the process, but not in enough detail to easily duplicate
it. Here is what we discovered after experimentation.
The basic form of the command is:
adb shell "am broadcast -a -d "
or you can run it within an dab shell on a device:
adb shell am broadcast -a -d
What should the <ACTION> and <DATA_URI> values be?
Action needs to be a recognized constant value from the Android documentation. I kept tripping myself up by using the constant name, rather than the value. Values are in the documentation for the constants:
The two that we found useful were android.intent.action.MEDIA_MOUNTED and android.intent.action.MEDIA_SCANNER_SCAN_FILE. As described in their respective documentation, MEDIA_MOUNTED takes a path to scan as its data_uri and MEDIA_SCANNER_SCAN_FILE takes a path
to a single file. For our use case, we ran the command:
adb -d shell "am broadcast -a android.intent.action.MEDIA_MOUNTED -d file:///mnt/sdcard/Movies/"
We had originally tried to use /mnt/sdcard/ as the full path, because it didn’t seem that there would be any harm in scanning the whole card. However, because different system apps watch paths on the sdcard, trying to say the whole card was mounted causes a
permissions error.
Because of that error, we had to be specify /Movies/, which did not trigger the error. If we do need to load music or images, we will be able to pass those specific paths with the same intent.
(Note: I’ve been unable to reproduce the permissions error more recently, so there is another factor involved. One difference could be that the tablets reporting the error were running 4.0.3, but we have since moved to tablets running 4.1.1.)
相关文章推荐
- Android内存泄漏的各种原因详解
- android内存管理机制(一)
- 从手机相册,相机选取照片
- Android内存管理机制详解
- 避免Android内存泄露
- android 权限管理的使用
- Android内存泄露案例分析
- 一个常见的android内存泄露 问题
- android 中在activity弹出一个对话框,并不会执行onpause生命周期
- Android 项目的代码混淆
- Android内存泄露原因分析与处理方案
- androidStudio引入自定义属性
- Android studio 百度地图开发(1)配置工程、显示地图
- Android开发资料:Android启动优化解析
- 在Android开发中,有哪些好的内存优化方式?如何避免 Out Of Memory(OOM) ?
- Android应用程序跳转到系统的各个设置页面
- Dagger2(初步探索一)
- Android攻城狮学习笔记-进阶篇一
- android APP logo 尺寸
- Android APK反编译就这么简单 详解(附图)