Java Android文档Provider setNotificationUri()和notifyChange()被忽略或使用错误
我正在写一个文档提供者,它是重缩放图像文件。如果加载了目录,则每个图像都会重新缩放,较小的版本会存储在cachedirectory中。如果缓存文件被替换,则假装要替换大文件 通常,这应该在每次完成重缩放后发生,但事实并非如此。只有在我重新进入目录时才会发生更改 我不知道我是否使用了Cursor.setNotificationUri()错误,或者它是否没有被触发 文件提供者的查询儿童文件:Java Android文档Provider setNotificationUri()和notifyChange()被忽略或使用错误,java,android,Java,Android,我正在写一个文档提供者,它是重缩放图像文件。如果加载了目录,则每个图像都会重新缩放,较小的版本会存储在cachedirectory中。如果缓存文件被替换,则假装要替换大文件 通常,这应该在每次完成重缩放后发生,但事实并非如此。只有在我重新进入目录时才会发生更改 我不知道我是否使用了Cursor.setNotificationUri()错误,或者它是否没有被触发 文件提供者的查询儿童文件: @Override public Cursor queryChildDocuments(String par
@Override
public Cursor queryChildDocuments(String parentDocumentId, String[] projection,
String sortOrder) throws FileNotFoundException {
final MatrixCursor result = new MatrixCursor(projection != null ?
projection : DEFAULT_DOCUMENT_PROJECTION) {
@Override
public Bundle getExtras() {
Bundle bundle = new Bundle();
bundle.putBoolean(DocumentsContract.EXTRA_LOADING, true);
return bundle;
}
};
final File parent = getFileForDocId(parentDocumentId);
for (File file : parent.listFiles()) {
String id = getDocIdForFile(file);
Uri updatedUri = DocumentsContract.buildChildDocumentsUri(
"xxx.xxx.de.resizeprovider.documents", id);
result.setNotificationUri(getContext().getContentResolver(), updatedUri);
includeFile(result, null, file, updatedUri);
}
return result;
}
包括:
private void includeFile(MatrixCursor result, String docId, File file, Uri updatedUri)
throws FileNotFoundException {
if (docId == null) {
docId = getDocIdForFile(file);
} else {
file = getFileForDocId(docId);
}
int flags = 0;
if (file.isDirectory()) {
// flags |= Document.FLAG_DIR_PREFERS_GRID;
if (file.isDirectory() && file.canWrite()) {
flags |= Document.FLAG_DIR_SUPPORTS_CREATE;
}
} else if (file.canWrite()) {
// If the file is writable set FLAG_SUPPORTS_WRITE and
// FLAG_SUPPORTS_DELETE
flags |= Document.FLAG_SUPPORTS_WRITE;
flags |= Document.FLAG_SUPPORTS_DELETE;
}
final String displayName = file.getName();
final String mimeType = getTypeForFile(file);
if (mimeType.startsWith("image/")) {
flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
File newfile = getFileForDocument(docId);;
boolean resize = false;
if (newfile.exists()){
file = newfile;
}
else {
new ResizeImageSimpleTask(file, newfile, resizeValue(), updatedUri).execute();
}
}
final MatrixCursor.RowBuilder row = result.newRow();
row.add(Document.COLUMN_DOCUMENT_ID, docId);
row.add(Document.COLUMN_DISPLAY_NAME, displayName);
row.add(Document.COLUMN_SIZE, file.length());
row.add(Document.COLUMN_MIME_TYPE, mimeType);
row.add(Document.COLUMN_LAST_MODIFIED, file.lastModified());
row.add(Document.COLUMN_FLAGS, flags);
// Add a custom icon
row.add(Document.COLUMN_ICON, R.mipmap.ic_launcher);
}
调整ImageSimpleTask的大小:
private class ResizeImageSimpleTask extends AsyncTask<Void, Void, Boolean> {
File oldfile = null;
File newfile = null;
int imageSimpleValue = 0;
Uri updatedUri = null;
private ResizeImageSimpleTask(File oldfile, File newfile, int imageSimpleValue, Uri updatedUri){
this.oldfile = oldfile;
this.newfile = newfile;
this.imageSimpleValue = imageSimpleValue;
this.updatedUri = updatedUri;
}
protected Boolean doInBackground(Void... params) {
return resizeImageSimple(oldfile, newfile, imageSimpleValue);
}
protected void onPostExecute(Boolean result) {
if (result ) {
Log.i("MyProvider", "Resize success");
getContext().getContentResolver().notifyChange(updatedUri, null);
}
}
}
私有类ResizeImageSimpleTask扩展异步任务{
文件oldfile=null;
File newfile=null;
int-imageSimpleValue=0;
uriupdateduri=null;
私有ResizeImageSimpleTask(文件oldfile、文件newfile、int-imageSimpleValue、Uri updateUri){
this.oldfile=oldfile;
this.newfile=newfile;
this.imageSimpleValue=imageSimpleValue;
this.updatedUri=updatedUri;
}
受保护的布尔doInBackground(Void…params){
返回resizeImageSimple(旧文件、新文件、imageSimpleValue);
}
受保护的void onPostExecute(布尔结果){
如果(结果){
Log.i(“MyProvider”、“调整大小成功”);
getContext().getContentResolver().notifyChange(updateUri,null);
}
}
}
Logcat显示Log.i(“MyProvider”,“Resize success”);所以我进入了onPostExecute(),但内容没有更新
写下我可以发送更改通知来触发重新查询,但我不确定这是否会再次调用queryChildDocuments(…)或执行其他操作
因为我喜欢支持Android 4.4+我只能使用api 19
我做错了什么或误解了什么