Android : Get recently added media in Android Device similarly to File Manager

半腔热情 提交于 2019-11-30 16:13:23

Here is my complete code to notify user when a new media (image, audio or video) is added into the device. It might help someone.Also, I am displaying push notifications as soon as new media is added.

EDITED (issues resolved are below)
1.Notify user only when media is added,not deleted.

public class MediaTrackerService extends Service {

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // images
    getContentResolver().registerContentObserver(
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            true, new ContentObserver(new Handler()) {
                @Override
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);

                    Media media = readFromMediaStore(
                            getApplicationContext(),
                            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    File file = new File(media.file.getPath());
                    Date fileData = new Date(file.lastModified());

                    if (System.currentTimeMillis() - fileData.getTime() > 3000) {

                    } else {

                        if (!media.file.getPath().contains("WhatsApp")) {
                            String saved = media.file.getName();

                            // deduce bitmap before using
                            Bitmap bitmap = BitmapFactory
                                    .decodeFile(media.file.getPath());

                            Intent intent = new Intent();
                            intent.setDataAndType(Uri.fromFile(media.file),
                                    "image/*");
                            PendingIntent pendingIntent = PendingIntent
                                    .getActivity(getApplicationContext(),
                                            0, intent, 0);

                            Notification n = new Notification.Builder(
                                    getApplicationContext())
                                    .setStyle(
                                            new Notification.BigPictureStyle()
                                                    .bigPicture(bitmap))
                                    .setContentTitle(
                                            "New Image File For You")
                                    .setContentText("File Name :" + saved)
                                    .setSmallIcon(R.drawable.alert_icon_1)
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true).build();

                            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                            notificationManager.notify(0, n);
                        }
                    }
                }

                @Override
                public void onChange(boolean selfChange, Uri uri) {
                    super.onChange(selfChange, uri);
                }
            });
    // audio
    getContentResolver().registerContentObserver(
            android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            true, new ContentObserver(new Handler()) {
                @Override
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);
                    Media media = readFromMediaStore(
                            getApplicationContext(),
                            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);

                    File file = new File(media.file.getPath());
                    Date fileData = new Date(file.lastModified());

                    if (System.currentTimeMillis() - fileData.getTime() > 3000) {

                    } else {

                        if (!media.file.getPath().contains("WhatsApp")) {

                            String saved = media.file.getName();

                            Bitmap bitmap = BitmapFactory
                                    .decodeFile(media.file.getPath());

                            Intent intent = new Intent();
                            intent.setDataAndType(Uri.fromFile(media.file),
                                    "audio/*");
                            PendingIntent pendingIntent = PendingIntent
                                    .getActivity(getApplicationContext(),
                                            0, intent, 0);

                            Notification n = new Notification.Builder(
                                    getApplicationContext())
                                    .setStyle(
                                            new Notification.BigPictureStyle()
                                                    .bigPicture(null))
                                    .setContentTitle(
                                            "New audio File For You")
                                    .setContentText("File Name :" + saved)
                                    .setSmallIcon(R.drawable.alert_icon_1)
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true).build();

                            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                            notificationManager.notify(0, n);
                        }
                    }
                }

                @Override
                public void onChange(boolean selfChange, Uri uri) {
                    super.onChange(selfChange, uri);
                }
            });
    // video
    getContentResolver().registerContentObserver(
            android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            true, new ContentObserver(new Handler()) {
                @Override
                public void onChange(boolean selfChange) {
                    super.onChange(selfChange);
                    Media media = readFromMediaStore(
                            getApplicationContext(),
                            MediaStore.Video.Media.EXTERNAL_CONTENT_URI);

                    File file = new File(media.file.getPath());
                    Date fileData = new Date(file.lastModified());

                    if (System.currentTimeMillis() - fileData.getTime() > 3000) {

                    } else {
                        if (!media.file.getPath().contains("WhatsApp")) {

                            String saved = media.file.getName();

                            Intent intent = new Intent();
                            intent.setDataAndType(Uri.fromFile(media.file),
                                    "video/*");
                            PendingIntent pendingIntent = PendingIntent
                                    .getActivity(getApplicationContext(),
                                            0, intent, 0);

                            Notification n = new Notification.Builder(
                                    getApplicationContext())
                                    .setStyle(
                                            new Notification.BigPictureStyle()
                                                    .bigPicture(null))
                                    .setContentTitle(
                                            "New Video File For You")
                                    .setContentText("File Name :" + saved)
                                    .setSmallIcon(R.drawable.alert_icon_1)
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true).build();

                            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

                            notificationManager.notify(0, n);
                        }
                    }
                }

                @Override
                public void onChange(boolean selfChange, Uri uri) {
                    super.onChange(selfChange, uri);
                    System.out
                            .println("here is the service change EXTERNAL 1 params"
                                    + " " + uri);
                }
            });
    return START_STICKY;
}

private Long readLastDateFromMediaStore(Context context, Uri uri) {
    Cursor cursor = context.getContentResolver().query(uri, null, null,
            null, "date_added DESC");
    Long dateAdded = (long) -1;
    if (cursor.moveToNext()) {
        dateAdded = cursor.getLong(cursor
                .getColumnIndexOrThrow(MediaColumns.DATE_ADDED));
    }
    cursor.close();
    return dateAdded;
}

private Media readFromMediaStore(Context context, Uri uri) {
    Cursor cursor = context.getContentResolver().query(uri, null, null,
            null, "date_added DESC");
    Media media = null;
    if (cursor.moveToNext()) {
        int dataColumn = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        String filePath = cursor.getString(dataColumn);
        int mimeTypeColumn = cursor
                .getColumnIndexOrThrow(MediaColumns.MIME_TYPE);
        String mimeType = cursor.getString(mimeTypeColumn);
        media = new Media(new File(filePath), mimeType);
    }
    cursor.close();
    return media;
}

private class Media {
    private File file;

    private String type;

    public Media(File file, String type) {
        this.file = file;
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public File getFile() {
        return file;
    }
}

}

and don't forget to unregister your observer onDestroy().

getContentResolver().unregisterContentObserver(yourObserverObject);

That is not possible in general. Your app has no rights to spy on the behavior of other apps. If the user, through my app, downloads something to my app's portion of internal storage, you cannot access that content (unless I choose to make it available) and you cannot find out that the content was added.

Beyond that, prior to API Level 24 (Android 7.0), there is no means for you accomplish this other than by trying to have a process running all the time. This is not good for the user, in terms of system RAM consumption and the like. Some users get very irritated when apps try to have a process run all of the time, and they may express their irritation in creative ways in the form of app store ratings.

All that being said, for publicly-accessible content (principally external storage), the simplest solution would be to set up a ContentObserver to monitor the MediaStore ContentProvider. On Android 7.0+, you could use JobScheduler to do the same thing, with the benefit that you do not need to keep your process running to keep the ContentObserver observing.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!