我已经将app-private文件夹中的文件复制到了Pictures
或者DCIM
我想在我的应用程序中打开库窗口小部件并显示这些图像.但是,我的gallery小部件使用MediaStore id创建了一个缩略图库,并且新添加的图像不会出现在那里.
我尝试了stackoverflow上建议的所有三个解决方案,以刷新媒体存储并告诉android有关新文件的存在
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
- 禁止在较新的API中使用
2.
MediaScannerConnection.scanFile(context, new String[]{ pathToFile1, pathToFile2 }, null, // tried also with a String[] with the mimetype, no difference new MediaScannerConnectionClient() { public void onMediaScannerConnected(){ } public void onScanCompleted(String path, Uri uri){ // URI is null, and the gallery doesn't display the image } });
3.
public static void scanFile(Context context, String path, String mimeType ) { Client client = new Client(path, mimeType); MediaScannerConnection connection = new MediaScannerConnection(context, client); client.connection = connection; connection.connect(); } private static final class Client implements MediaScannerConnectionClient { private final String path; private final String mimeType; MediaScannerConnection connection; public Client(String path, String mimeType) { this.path = path; this.mimeType = mimeType; } @Override public void onMediaScannerConnected() { connection.scanFile(path, mimeType); } @Override public void onScanCompleted(String path, Uri uri) { connection.disconnect(); } }
再次,uri为空
Android为何如此难以执行如此正常的合法行为?如何在牛轧糖中达到这种效果?
编辑:我也试过发送广播 ACTION_MEDIA_SCANNER_SCAN_FILE
我甚至考虑到了这一点:https: //inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en
所以现在我发送content://
而不是file://
URI,但仍然没有!
EDIT2:
我试过这个
public static void scheduleJob(Context context) { JobScheduler js = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); JobInfo job = new JobInfo.Builder( MY_BACKGROUND_JOB, new ComponentName(context, MyJobService.class)) .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) .setRequiresCharging(true) .build(); js.schedule(job); }
如此处所述
https://developer.android.com/topic/performance/background-optimization.html
但是,当我打开我的画廊时,新的图像不在那里