Hi i am trying to refresh gallery by initiating a scan. For devices running Kitkat and higher versions i am using mediascannerconnection. But the mediaScannerConnection needs absolute path to do scan. What i have is a DocumentFile which does not seem to have any method to get the absolute path of it.
Below is the code i am using:
To get permission to write to the folder using the new lollipop API-->Saving the selected URI to a global variable---> Logic to hide(create/delete ".nomedia" file).
// Calling folder selector.
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
startActivityForResult(intent, 42);
..............
@SuppressLint("NewApi") @Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
if (resultCode == RESULT_OK && requestCode == 42) {
Uri treeUri = resultData.getData();
getContentResolver().takePersistableUriPermission(treeUri,
// Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
path = treeUri.getPath();
}
}
..........
public void hide(boolean hide){
DocumentFile pickedDir = DocumentFile.fromTreeUri(this,Uri.parse(path));
if(hide){
if(pickedDir.findFile(".nomedia")==null){
pickedDir.createFile(null, ".nomedia");
tellgallery(pickedDir.findFile(".nomedia").getUri().getPath());
}
}
else{
DocumentFile filetodelete = pickedDir.findFile(".nomedia");
if(filetodelete!=null){
filetodelete.delete();
tellgallery(filetodelete.getPath());
}
}
}
This code above works perfectly and creates/deletes a ".nomedia" file in the desired folder.
Now when i try to tell the gallery that files have changed for this folder. Gallery is not able to pick the change.
Below is the code to refresh/request the scan.
private void tellgallery(String path) {
pathtonomedia = path;
if(path!=null){
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
mediaScannerConnection = new MediaScannerConnection(con,mediaScannerConnectionClient);
mediaScannerConnection.connect();
}
else{
this.sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + path)));
}
}
}
MediaScanner class is as below:
private MediaScannerConnectionClient mediaScannerConnectionClient =
new MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() {
mediaScannerConnection.scanFile(pathtonomedia, null);
}
@Override
public void onScanCompleted(String path, Uri uri) {
if(path.equals(pathtonomedia))
mediaScannerConnection.disconnect();
}
};
But the code does not refresh the gallery and i can still see the folder in gallery. The folder however vanishes after sometime like 10 to 20 mins may be by system refresh.
Finally what i want is
Is there anyway i can get path of the DocumentFile instance?. So that i can just directly pass it to the mediascanner path
Or else is there any other method by which we can request a scan on specific folder in lollipop.The specific folder being in SDCARD and i know only the URI to it not the actual path.
Any help is highly appreciated.