Android : 在 Android 设备中获取最近添加的媒体,类似于文件管理器
Android : Get recently added media in Android Device similarly to File Manager
我正在开发一个应用程序,我必须在用户以任何方式(无论是下载还是传输)插入新图像、视频、音频或 apk 时立即通知用户,类似于什么文件管理器应用程序 do.I 不知道从哪里开始。谢谢
这在一般情况下是不可能的。您的应用无权监视其他应用的行为。如果用户通过我的应用程序将某些内容下载到我的应用程序的 internal storage 部分,您将无法访问该内容(除非我选择使其可用)并且您无法发现该内容已添加。
除此之外,在 API 级别 24 (Android 7.0) 之前,除了尝试让一个过程 运行时间。就系统 RAM 消耗等而言,这对用户不利。当应用程序一直试图拥有一个进程时,一些用户会非常恼火 运行,他们可能会以应用商店评级的形式以创造性的方式表达他们的愤怒。
综上所述,对于可公开访问的内容(主要是 external storage),最简单的解决方案是设置一个 ContentObserver
来监控 MediaStore
ContentProvider
.在 Android 7.0+ 上,您可以使用 JobScheduler
做同样的事情,好处是您不需要保持进程 运行 保持 ContentObserver
观察。
这是我的完整代码,用于在设备中添加新媒体(图像、音频或视频)时通知用户。这可能会有所帮助 someone.Also,我会在添加新媒体后立即显示推送通知。
已编辑(已解决的问题如下)
1.Notify 用户仅在添加媒体时使用,不会删除。
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()
。
getContentResolver().unregisterContentObserver(yourObserverObject);
我正在开发一个应用程序,我必须在用户以任何方式(无论是下载还是传输)插入新图像、视频、音频或 apk 时立即通知用户,类似于什么文件管理器应用程序 do.I 不知道从哪里开始。谢谢
这在一般情况下是不可能的。您的应用无权监视其他应用的行为。如果用户通过我的应用程序将某些内容下载到我的应用程序的 internal storage 部分,您将无法访问该内容(除非我选择使其可用)并且您无法发现该内容已添加。
除此之外,在 API 级别 24 (Android 7.0) 之前,除了尝试让一个过程 运行时间。就系统 RAM 消耗等而言,这对用户不利。当应用程序一直试图拥有一个进程时,一些用户会非常恼火 运行,他们可能会以应用商店评级的形式以创造性的方式表达他们的愤怒。
综上所述,对于可公开访问的内容(主要是 external storage),最简单的解决方案是设置一个 ContentObserver
来监控 MediaStore
ContentProvider
.在 Android 7.0+ 上,您可以使用 JobScheduler
做同样的事情,好处是您不需要保持进程 运行 保持 ContentObserver
观察。
这是我的完整代码,用于在设备中添加新媒体(图像、音频或视频)时通知用户。这可能会有所帮助 someone.Also,我会在添加新媒体后立即显示推送通知。
已编辑(已解决的问题如下)
1.Notify 用户仅在添加媒体时使用,不会删除。
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()
。
getContentResolver().unregisterContentObserver(yourObserverObject);