Backlight87
8/24/2017 - 2:32 PM

图片工具类,包括传入图片地址(一般是缓存图片)保存图片到相册

图片工具类,包括传入图片地址(一般是缓存图片)保存图片到相册

public class ImageUtils {

    public static boolean saveToAlbum(String filePath, Context context) {
        try {
            File file = new File(filePath);
            String videoName = file.getName();
            File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            File fileNew = new File(dcim, videoName);
            FileUtils.copyFile(file, fileNew);
            //发送广播更新media store
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                final Uri contentUri = Uri.fromFile(fileNew);
                scanIntent.setData(contentUri);
                context.sendBroadcast(scanIntent);
            } else {
                final Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + dcim));
                context.sendBroadcast(intent);
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }
}