重启后无法获取 MediaStore.Images.Media

发布于 2025-01-12 00:21:09 字数 2001 浏览 4 评论 0原文

我开发了一个应用程序,它将照片保存在应用程序文件夹(/android/data/[app-folder]/files/Report/[directory])中,并将一些数据保存到描述字段中,这是我保存图像的代码:

ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, directory);
image.put(Images.Media.DISPLAY_NAME, FOTOC_PREFIX_NAME + "_" + directory + "_" + data + ".jpg");
image.put(Images.Media.DESCRIPTION, "EXAMPLE DESC");
image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
image.put(Images.Media.MIME_TYPE, "image/jpg");
image.put(Images.Media.ORIENTATION, 0);
File parent = imagePath.getParentFile();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
 image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, parent.getAbsolutePath());
}
image.put(Images.ImageColumns.BUCKET_ID, parent.toString().toLowerCase().hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName().toLowerCase());
image.put(Images.Media.SIZE, imagePath.length());
image.put(Images.Media.DATA, imagePath.getAbsolutePath());
Uri result = getContentResolver().insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), image);

然后我读取数据:

String whereCause = MediaStore.Images.Media.DISPLAY_NAME + " LIKE '%" + UsefullThings.getNameWithoutExtension(photofile.getName()) + "%'";
String[] projection = new String[] {MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DESCRIPTION,MediaStore.Images.Media.TITLE,MediaStore.Images.Media._ID};
 Cursor cursor = getContentResolver().query(MediaStore.Images.Media
                                                            .getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), projection, whereCause, null, MediaStore.Images.Media._ID);
 if (cursor.getCount() == 1) {
   cursor.moveToFirst();
 }

 String descstring = "";
 try {
  descstring =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DESCRIPTION));
 }catch(Exception e){
  ....
 }

其中 photofile 是图像。 一切工作正常,但如果我重新启动平板电脑,则相同的查询将返回空光标。

谁能帮助我吗? 谢谢

I developed an app which saves photos inside the app folder (/android/data/[app-folder]/files/Report/[directory]) with some data saved into DESCRIPTION field, here is my code to save image:

ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, directory);
image.put(Images.Media.DISPLAY_NAME, FOTOC_PREFIX_NAME + "_" + directory + "_" + data + ".jpg");
image.put(Images.Media.DESCRIPTION, "EXAMPLE DESC");
image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
image.put(Images.Media.MIME_TYPE, "image/jpg");
image.put(Images.Media.ORIENTATION, 0);
File parent = imagePath.getParentFile();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
 image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, parent.getAbsolutePath());
}
image.put(Images.ImageColumns.BUCKET_ID, parent.toString().toLowerCase().hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName().toLowerCase());
image.put(Images.Media.SIZE, imagePath.length());
image.put(Images.Media.DATA, imagePath.getAbsolutePath());
Uri result = getContentResolver().insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), image);

And then i read data with:

String whereCause = MediaStore.Images.Media.DISPLAY_NAME + " LIKE '%" + UsefullThings.getNameWithoutExtension(photofile.getName()) + "%'";
String[] projection = new String[] {MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DESCRIPTION,MediaStore.Images.Media.TITLE,MediaStore.Images.Media._ID};
 Cursor cursor = getContentResolver().query(MediaStore.Images.Media
                                                            .getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), projection, whereCause, null, MediaStore.Images.Media._ID);
 if (cursor.getCount() == 1) {
   cursor.moveToFirst();
 }

 String descstring = "";
 try {
  descstring =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DESCRIPTION));
 }catch(Exception e){
  ....
 }

where photofile is the image.
Everything is working fine, but if i reboot my tablet then the same query returns empty cursor.

Can anyone help me?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-01-19 00:21:10

<块引用>

将照片保存在应用程序文件夹(/android/data/[app-folder]/files/Report/[directory])内的应用程序

你的意思是 /storage/emulated/0/Android/data/[app-folder]/files/Report /[目录] ?

如果您使用 MediaStore,这是不可能开始的。

这是一个特定于应用程序的目录,MediaStore 不会为您提供该位置的 insert() uri。

<块引用>

image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH,parent.getAbsolutePath());

您应该在 .RELATIVE_PATH 中放置相对路径。不是像现在这样的完整绝对路径。我们也看不到你到底放了什么。请告知parent.getAbsolutePath()的值。

app which saves photos inside the app folder (/android/data/[app-folder]/files/Report/[directory])

You mean /storage/emulated/0/Android/data/[app-folder]/files/Report/[directory] ?

That is impossible to begin with if you use the MediaStore.

That is an app specific directory and the MediaStore will not give you insert() uries for that location.

image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, parent.getAbsolutePath());

You should put a relative path in .RELATIVE_PATH. Not a full absolute path as you do now. Also we cannot see what exactly you put in there. Please tell value of parent.getAbsolutePath().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文