Why MediaScannerConnection.scanFile(this, new String[] { uri_path_or_subfolder }, null,... ) was not deprecated in AndroidQ,R (SDK29,30)?

Viewed 316

Why MediaScannerConnection.scanFile(this, new String[] { uri_path_or_subfolder }, null,... ) was not deprecated in AndroidQ,R (SDK29,30)?

I have made a small photo stored in Pictures/subfolderABC with

final Uri contentUri = MediaStore.Images.Media.getContentUri("external");
Uri uri = resolver.insert(contentUri, contentValues);
//Then ... Do some works on the uri with bitmap , ParcelFileDescriptor and FileOutputStream

I confirmed that uri is not null by checking uri.toString();

System.out.println(uri.toString());

Debug Dialogue show: content://media/external/images/media/85

In Android SDK<=28, I will usually use

MediaScannerConnection.scanFile(this,
     new String[] { file.toString()}, null,
     new MediaScannerConnection.OnScanCompletedListener() {
                                public void onScanCompleted(String path, Uri uri_local) {
                                    Log.i("ExternalStorage", "Scanned " + path + ":");
                                    Log.i("ExternalStorage", "-> uri=" + uri_local);
                                }
                            });

to let the user can find the image created quickly when user open the gallery and no need to wait.

So I attempt to do so in Android Q and R , (SDK 29,30) with similar ways...

String testingString;
switch (testingCaseNum){

    case 1: testingString = contentUri.toString(); break; //testing1
    case 2: testingString = uri.toString(); break; //testing2
    case 3: testingString = "content://media/external/images/media"; break; //testing3, should be same as 
    case 4: testingString = relativePath; break; //testing4 //relativePath = "Pictures/subfolderABC"
    case 5: testingString = "Pictures"; break; //testing5

    default:testingString = contentUri.toString(); break;
}

String testingString = contentUri.toString(); //testing
MediaScannerConnection.scanFile(this,
     new String[] {tesingString}, null,
     new MediaScannerConnection.OnScanCompletedListener() {
         public void onScanCompleted(String path, Uri uri_local) {
             Log.i("ExternalStorage", "Scanned " + path + ":");
             Log.i("ExternalStorage", "-> uri=" + uri_local);
         }
     });

The debug dialogue shows:

D/MediaScannerConnection: Scanned xxxx to null
I/ExternalStorage: Scanned xxxx:
I/ExternalStorage: -> uri=null

I used about 12-18 hours to find the problem why the emulator return a null uri but at the upper codes the image has a uri with id generated! I supposed that I input a wrong String for telling AndroidQ where to scan. But in 1 hour before, I find an eassy summarizing some problems Android Q in 2019 by a developer. He said that all String paths you enter will return null if you put uri!!!!!! Android Studio didn't pump any hint or message telling me MediaScannerConnection.scanFile is not working or deprecated in AndroidQ in Oct 2020 ???? This wastes my time, do Android Studio really know there is problem about scanFile?

0 Answers
Related