简体   繁体   English

为什么查询Androids MediaStore会返回空光标?

[英]Why querying Androids MediaStore returns empty cursor?

I'm using Android's MediaStore to retrieve duration/dateTaken of video files I record with my app. 我正在使用Android的MediaStore来检索我用我的应用程序录制的视频文件的持续时间/日期。 It works fine for older files, latest files I record do return empty (not null) cursor though. 它适用于较旧的文件,我记录的最新文件确实返回空(非空)光标。 I cant see why this is happening, is MediaStore not updating instantly or there is a problem with the code I use below? 我不知道为什么会发生这种情况,MediaStore是不是立即更新还是我下面使用的代码有问题? Thanks for any help! 谢谢你的帮助!

Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.MediaColumns.DISPLAY_NAME + " LIKE '"+file.getName()+"'";
Cursor cursor = getContentResolver().query(uri, new String[]{MediaStore.Video.VideoColumns.DATE_TAKEN,MediaStore.Video.VideoColumns.DURATION}, selection, null, null);

I was also facing the same problem. 我也面临同样的问题。 Printing the stacktrace revieled the issue. 打印堆栈跟踪重新启动了该问题。 Please add this permission in Manifest: 请在Manifest中添加此权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Seems like MediaScanner didn't scan latest files into MediaStore yet. 似乎MediaScanner尚未将最新文件扫描到MediaStore中。 You have to trigger if there are new files in your external storage. 如果外部存储中有新文件,则必须触发。

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));

Check to make sure that having your Android device plugged in isn't messing with the external storage. 检查以确保插入Android设备不会弄乱外部存储。 If your phone is mounting as USB storage, then you won't have direct access to the files in external storage. 如果您的手机正在安装为USB存储设备,则您将无法直接访问外部存储设备中的文件。 You can check for this by opening the gallery while your phone is connected to your computer. 您可以在手机连接到计算机时打开图库来检查此项。

I just went through developing an app that had to access all of the pictures on the phone, and it doesn't work while the phone is plugged in. However, it works just fine if I unplug it. 我刚刚开发了一个必须访问手机上所有图片的应用程序,并且在插入手机时它无法正常工作。但是,如果我拔掉它,它的工作正常。

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

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