miao1007 / AnimeWallpaper

[Android] Konachan wallpaper downloader
http://www.jianshu.com/p/93475df308b8
429 stars 84 forks source link

我给你修复了一个BUG #13

Closed Lightning627 closed 6 years ago

Lightning627 commented 6 years ago

下载完壁纸之后发送广播手动刷新图库:

`/**
     * 刷新图库
     *
     * @param context
     * @param file
     * @param fileName
     */
    public static void refreshAlbum(Context context, File file, String fileName) {
        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(),
                    file.getPath(), fileName, null);
            // 最后通知图库更新
//        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }`

设置壁纸的代码会崩溃,我改成这样子了:

@TargetApi(Build.VERSION_CODES.KITKAT)
    private static void setWallpaperKitkat(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra("mimeType", "image/jpg");
        Uri uri = Uri.parse(MediaStore.Images.Media
                .insertImage(context.getContentResolver(),
                        BitmapFactory.decodeFile(file.getAbsolutePath()), null, null));
        intent.setData(uri);
        try {
            if (Build.VERSION.SDK_INT >= 24) {
                try {
                    Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                    m.invoke(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            context.startActivity(intent);
        } catch (ActivityNotFoundException e) {
            e.printStackTrace();
        }
    }

另外BaseUrl后缀改成.com结尾可以获取18X的图片,但是要翻墙。