ich diesen Code bin mit einem Bild kopieren mit documentFile.createFile()
ContentResolver enthält nicht nur erstellt Bild in einer unmittelbaren Abfrage nach der Erstellung der Datei
private void newcopyFile(File fileInput, String outputParentPath,
String mimeType, String newFileName) {
DocumentFile documentFileGoal = DocumentFile.fromTreeUri(this, treeUri);
String[] parts = outputParentPath.split("\\/");
for (int i = 3; i < parts.length; i++) {
if (documentFileGoal != null) {
documentFileGoal = documentFileGoal.findFile(parts[i]);
}
}
if (documentFileGoal == null) {
Toast.makeText(MainActivity.this, "Directory not found", Toast.LENGTH_SHORT).show();
return;
}
DocumentFile documentFileNewFile = documentFileGoal.createFile(mimeType, newFileName);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
outputStream = getContentResolver().openOutputStream(documentFileNewFile.getUri());
inputStream = new FileInputStream(fileInput);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (outputStream != null) {
byte[] buffer = new byte[1024];
int read;
if (inputStream != null) {
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
}
if (inputStream != null) {
inputStream.close();
}
inputStream = null;
outputStream.flush();
outputStream.close();
outputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
Und das ist, wie ich Abfrage ContentResolver
nach Bild erzeugt wird, zu Aktualisieren Sie sofort meine Bildergalerie mit dem Ergebnis der Abfrage, die Informationen zum neu erstellten Bild enthalten soll.
cursorPhotos = MainActivity.this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projectionsImages,
null,
null,
MediaStore.Images.Media.DATE_TAKEN + " DESC"
);
Aber sofortige Abfrage konnte neu erstelltes Bild nicht finden. Und wenn ich die Abfrage nach einem Moment erneut ausführen, ist das neu erstellte Bild im Ergebnis vorhanden.
Es scheint, dass Bereitstellung von Informationen für neu erstellte Bild dauert ContentResolver
(wenn ContentResolver verantwortlich dafür ist) als würde im Hintergrund ausgeführt werden, während ich sofortige Abfrage ausführen.
Gibt es eine Methode oder einen Listener zu wissen, wenn das neu erstellte Bild von ContentResolver
registriert wird?
Kopieren Sie die Bilder in eine Async-Aufgabe oder in den Haupt-UI-Thread? – Rahulrr2602
@ Rahulrr2602 Ja, siehe die Antwort. – Eftekhari
Vielen Dank. Wenn ich die newcopyFile-Methode viele Male wiederhole, dann ist manchmal documentFileGoal gleich null. – Rahulrr2602