Android:类似于文件管理器,在Android设备中获取最近添加的媒体 [英] Android : Get recently added media in Android Device similarly to File Manager

查看:90
本文介绍了Android:类似于文件管理器,在Android设备中获取最近添加的媒体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,在该应用程序中,用户必须以任何方式(无论他是下载还是传输)插入新的图像,视频,音频或apk(无论是下载还是传输),都必须通知用户经理应用程序可以.我不知道从哪里开始.谢谢

I am working on an application in which I have to notify user as soon as a new image, video, audio or apk is inserted by the user by any means (whether he downloads it or transfer it),similar to what File Manager application do.I don't know where to start from. Thanks

推荐答案

这是我的完整代码,用于在向设备添加新媒体(图像,音频或视频)时通知用户.这可能会对某人有所帮助.此外,添加新媒体后,我会立即显示推送通知.

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.

已编辑(已解决的问题在下面)
1.仅在添加媒体但不删除媒体时通知用户.

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

}

并且不要忘记在onDestroy()上注销您的观察者的注册.

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

getContentResolver().unregisterContentObserver(yourObserverObject);

这篇关于Android:类似于文件管理器,在Android设备中获取最近添加的媒体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆