gpt4 book ai didi

java - 有没有办法在图像从外部存储中删除后立即获取图像的 uri?

转载 作者:太空宇宙 更新时间:2023-11-04 09:43:17 25 4
gpt4 key购买 nike

我正在编写一个图库应用程序,当应用程序第一次启动时,它会扫描整个外部存储以检索所有图像 Uris 并将它们存储在数据库中,以便以后启动时它可以从自己的数据库加载它们。现在我的问题是,当图像被删除时,如何获取删除的图像Uri来通知图库并更新数据库。

当通过相机添加新图像时,我尝试了 JobScheular,效果非常好。

这是代码。

public class MediaJobSchedulerService extends JobService {

private static final int ASJOBSERVICE_JOB_ID = 999;

// A pre-built JobInfo we use for scheduling our job.
private static JobInfo JOB_INFO = null;

public static int a(Context context) {
int schedule = (context.getSystemService(JobScheduler.class)).schedule(JOB_INFO);
Log.i("PhotosContentJob", "JOB SCHEDULED!");
return schedule;
}

// Schedule this job, replace any existing one.
public static void scheduleJob(Context context) {
if (JOB_INFO != null) {
a(context);
} else {
JobScheduler js = context.getSystemService(JobScheduler.class);
JobInfo.Builder builder = new JobInfo.Builder(ASJOBSERVICE_JOB_ID,
new ComponentName(context, MediaJobSchedulerService.class));
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1));
builder.addTriggerContentUri(new JobInfo.TriggerContentUri(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 1));
builder.setTriggerContentMaxDelay(500);
JOB_INFO = builder.build();
js.schedule(JOB_INFO);
}
}


@Override
public boolean onStartJob(final JobParameters params) {
// Did we trigger due to a content change?
final Context context = this;
if (params.getTriggeredContentAuthorities() != null) {
if (params.getTriggeredContentUris() != null) {
// If we have details about which URIs changed, then iterate through them
// and collect either the ids that were impacted or note that a generic
// change has happened.
final Repository repo = Repository.getInstance(this);
ArrayList<String> ids = new ArrayList<>();
for (final Uri uri : params.getTriggeredContentUris()) {
if (uri != null) {

Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {

if (!uri.toString().equals("content://media/external")) {
Log.i("NEW_MEDIA", getRealPathFromUri(context, uri));
repo.addImage(getRealPathFromUri(context, uri));
}
}
});
}
}
jobFinished(params, true); // see this, we are saying we just finished the job
// We will emulate taking some time to do this work, so we can see batching happen.
scheduleJob(this);
}
}
return true;
}

@Override
public boolean onStopJob(JobParameters params) {
return false;
}

public static String getRealPathFromUri(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = {MediaStore.Images.Media.DATA};
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}

}

但如果图像被删除,Uri 就会丢失。

非常感谢您的帮助。

最佳答案

Android 没有用于此目的的事件,但您可以使用 FileObserver。这将捕获文件的删除。示例:

import android.os.FileObserver;
public class FileDeletedObserver extends FileObserver {
public String absolutePath;
public FileDeletedObserver(String path) {
super(path, FileObserver.DELETE);
absolutePath = path;
}
@Override
public void onEvent(int event, String path) {
if (path == null) {
return;
}

if ((FileObserver.DELETE & event)!=0) {
//handle deleted file
}
}
}

关于java - 有没有办法在图像从外部存储中删除后立即获取图像的 uri?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55723355/

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