current position:Home>After Android takes a photo, the notification to update the album has no effect. Searching the whole network turns out to be the reason

After Android takes a photo, the notification to update the album has no effect. Searching the whole network turns out to be the reason

2022-07-31 02:19:47Tommy porridge

This time is a project to develop native plug-ins for uniAPP
During the development process, I found that after saving the pictures and videos captured in my app, there are no pictures and videos in the album. I have to go to the folder to find it, from the perspective of the product.This is very unfriendly to users.So we need to notify the photo album to scan the place where we save the file.
The commonly used API in the lower version is to update by broadcasting, for example (the code comes from the post I have read, and the address link is attached):

String path = "File path";
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(new File(path));
intent.setData(uri);
activity.sendBroadcast(intent);

(https://blog.csdn.net/xing_xiangrong/article/details/94602602)
This is what I use in the lower version APIThe method is available for personal testing, but in the higher version API you will find that ACTION_MEDIA_SCANN
ER_SCAN_FILE has been deprecated.
But my current plug-in project is a high version of API 30 (in fact, my nc set the api to 30), and then there are many strange problems.For example, the above method will not work.Then I checked a lot of similar methods on the Internet one after another, but none of them were implemented.After saving, there is no way to notify the album update. Here I recommend a good article that I saw written, https://blog.csdn.net/xiayiye5/article/details/117221591
I believe you have to ask,Does this article work?The result is: I tested it according to the article here.The code of the article can run, but it can't be moved to my project!Don't talk nonsense and go directly to the final part:
I just thought, the above article is basically based on the cv, why not?I checked step by step, and finally found that the paths we store are different.I store it in the directory of getExternalFilesDir(), that is, under the android/data/data/ package name.OK, here comes the magic.I resolutely guessed that it was a problem after the android upgrade. I tested it with an emulator with an API lower than 29. Sure enough, the album was refreshed successfully!!!
Now that you have found the problem, go to the official documentation!As follows:

Okay, API29 is also Android11 ​​mandatory partition, although it is officially recommended to store external files under the android/data/data/ package name, but if it exists in this place, using MediaScannerConnection.scanFile() cannot find it and cannot notify the album to updateNow, there is no way, I have to use the method of getExternalStorageDirectory() to store my new folder externally to store it (ps: mainly set android:requestLegacyExternalStorage="true" in AndroidManifest, otherwise you will not be able to access).

Summary:
1 Don't mess with upgrading targetSdkVersion!!!
2 The whole thing is because the directory created by getExternalFilesDir() cannot be found by the media scanner on machines higher than 29
3 In shortMy current method is to change the path of the storage file to the outside (the file that cannot be accessed under the non-android/data/data/ package name will do).
4 android:requestLegacyExternalStorage="true" is only useful for api29, not required for low version, not applicable to high version, it is only used to transition api29, so if your targetSdkVersion is greater than 29, creating a folder will still fail, I alsoI don't know what to do, I will attach it later when I know it, and it will come to an end for the time being.
ps: I hope the old brother who knows will reply.common progress.
 
原文链接:https://blog.csdn.net/qq_37210897/article/details/119803043

copyright notice
author[Tommy porridge],Please bring the original link to reprint, thank you.
https://en.hqmana.com/2022/212/202207310117003128.html

Random recommended