gpt4 book ai didi

android - ContentResolver 在创建文件后立即查询中不包含刚刚创建的图像

转载 作者:行者123 更新时间:2023-11-30 01:09:33 27 4
gpt4 key购买 nike

我使用此代码通过 documentFile.createFile() 复制图像

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();
}
}

这就是我在创建图像后查询 ContentResolver 的方式,以立即使用包含新创建图像信息的查询结果刷新我的图像库。

cursorPhotos = MainActivity.this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projectionsImages,
null,
null,
MediaStore.Images.Media.DATE_TAKEN + " DESC"
);

但是立即查询找不到新创建的图像。如果我稍后再次运行查询,结果中会出现新创建的图像。

似乎为新创建的图像提供信息需要 ContentResolver(如果 ContentResolver 负责),因为它会在我运行即时查询时在后台运行。

是否有任何方法或监听器可以知道新创建的图像何时被 ContentResolver 注册?

最佳答案

你可以使用 this或者这就是我如何为 ContentResolver 更改实现监听器(观察器),使用 ContentObserver 来了解何时是运行查询以从中获取新创建图像的适当时间ContentResolver

首先创建ContentObserver类:

这个类正如它的名字告诉我们的,观察我们想要的 Uri 中的任何内容变化。

class MyObserver extends ContentObserver {
public MyObserver(android.os.Handler handler) {
super(handler);
}

@Override
public void onChange(boolean selfChange) {
this.onChange(selfChange, null);
}

@Override
public void onChange(boolean selfChange, Uri uri) {
//(SDK>=16)
// do s.th.
// depending on the handler you might be on the UI
// thread, so be cautious!

// This is my AsyncTask that queries ContentResolver which now
// is aware of newly created media file.
// You implement your own query here in whatever way you like
// This query will contain info for newly created image
asyncTaskGetPhotosVideos = new AsyncTaskGetPhotosVideos();
asyncTaskGetPhotosVideos.execute();
}
}

在复制方法的最后,您可以将 ContentObserver 设置为特定 Uri 上的 ContentResolver

 getContentResolver().registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true,
myObserver);

并且不要忘记注销您的观察者,否则您将面临内存泄漏。我更愿意在我的 AsyncTask (onPostExecute) 结束时执行此操作。

getContentResolver().unregisterContentObserver(myObserver);

您可以选择在整个应用程序生命周期内在所需的 Uri 上设置一个 ContentObserver,以便在媒体从您的应用程序外部或内部更改、删除或插入时收到通知。

对于这种方法,您可以在 onResume() 生命周期方法中注册您的观察者,并在 onPause() 方法中注销它。

关于android - ContentResolver 在创建文件后立即查询中不包含刚刚创建的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38554843/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com