简体   繁体   English

访问内部存储器上的所有媒体文件

[英]Access All Media Files on Internal Storage

I'm using the following example to try to load saved images and video on an android device into a customized gallery activity: 我正在使用以下示例尝试将Android设备上保存的图像和视频加载到自定义的图库活动中:

Access ordered images and video in same Cursor 访问同一光标中的有序图像和视频

While this works for images and video created using the default Android camera app, it doesn't seem to find media files that are saved locally in a folder specifically for the app (located in Pictures/Name_of_app) 虽然这适用于使用默认Android相机应用程序创建的图像和视频,但似乎找不到在本地存储在该应用程序专用文件夹中的媒体文件(位于Pictures / Name_of_app中)

How can I fetch all media files that are on the device? 如何获取设备上的所有媒体文件?

private String[] projection = {
            MediaStore.Files.FileColumns._ID, 
            MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.DATE_ADDED,
            MediaStore.Files.FileColumns.MEDIA_TYPE,
            MediaStore.Files.FileColumns.MIME_TYPE,
            MediaStore.Files.FileColumns.TITLE
    }; // used for query ContentResolver for mediafiles

    private String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
            + " OR " 
            + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;  // used to select images and videos from contentResolver

    private Uri queryUri = MediaStore.Files.getContentUri("external");  

 cl = new CursorLoader(CustomizedGallery.this, queryUri, projection, selection, null, MediaStore.Files.FileColumns.DATE_ADDED + " DESC");

It sounds like the files haven't been added to the MediaStore database yet. 听起来文件还没有添加到MediaStore数据库中。 You can check this by checking the gallery app - if they don't show up there they aren't in the DB yet. 您可以通过检查图库应用程序来检查此内容-如果它们未显示在数据库中,则说明它们还不在数据库中。

Simply creating them does not automatically add them to the database - scans happen at regular trigger points(such as on reboot or remount of the external storage) or you can manually request a rescan. 仅仅创建它们并不会自动将它们添加到数据库中-扫描会在常规触发点(例如在重新启动或重新安装外部存储时)进行,或者您可以手动请求重新扫描。

To request a scan of a particular file you can call: 要请求扫描特定文件,您可以调用:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(newFile)));

More information here: How can I refresh MediaStore on Android? 此处的更多信息: 如何在Android上刷新MediaStore?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM