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

标签 java android

我正在编写一个图库应用程序,当应用程序第一次启动时,它会扫描整个外部存储以检索所有图像 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/

相关文章:

java - Tomcat 应用程序 404 无法加载 - 上下文路径中的应用程序无法启动

java - 电锯 v2 中的存储大小

java - 在 HashMap Key 中输入 3 个属性

java - 使用准备好的语句查询列表 (JDBC)

java - 在 Android 上确定 SD 卡上目录大小的最快方法

java - Android:下载 flickr 图像

java - Maven-surefire-plugin 测试在 Jenkins 构建中失败但在本地运行成功?

android - 有没有RecyclerView等价于ListView的transcriptMode alwaysScroll?

Android LocationListener 没有命中 onLocationChanged

java - 为什么不能使用 new 运算符从 AudioManager 实例化对象?