文件下载并存图片格式,并在图库显示

前提

是:compile ‘com.liulishuo.filedownloader:library:1.6.8’ ##

private void download() {
FileDownloader.setup(PhotosDetailActivity.this);

System.out.println("绝对路径:"+FileUtils.createSDRootPath(PhotosDetailActivity.this));
System.out.println("绝对路径:+11111111111111111");
System.out.println("绝对路径:"+FileUtils.getImageSDPath(PhotosDetailActivity.this));
String url = getIntent().getStringExtra(AppConstant.PHOTO_DETAIL);
System.out.println("url:"+url);
FileDownloader.getImpl().create(url)
        .setPath(FileUtils.getImageSDPath(PhotosDetailActivity.this),true)
        .setListener(new FileDownloadListener() {
            @Override
            protected void pending(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                System.out.println("pending");
            }

            @Override
            protected void connected(BaseDownloadTask task, String etag, boolean isContinue, int soFarBytes, int totalBytes) {
                System.out.println("conneted:");
            }

            @Override
            protected void progress(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                System.out.println("progress:");
            }

            @Override
            protected void blockComplete(BaseDownloadTask task) {
                System.out.println("blockcomplet:");
            }

            @Override
            protected void retry(final BaseDownloadTask task, final Throwable ex, final int retryingTimes, final int soFarBytes) {
                System.out.println("retry:");
            }

            @Override
            protected void completed(BaseDownloadTask task) {
                ToastUitl.showShort("下载完成");
                System.out.println("getTargetFilePath:"+task.getTargetFilePath());
                System.out.println("getFilename:"+task.getFilename());

                try {
                    File file = new File(FileUtils.getImageSDPath(PhotosDetailActivity.this)+File.separator + task.getFilename() + ".jpg");
                    System.out.println("file.getPath():"+file.getPath());
                    Bitmap mBitmap=  BitmapFactory.decodeFile(task.getTargetFilePath());
                    FileOutputStream out = new FileOutputStream(file);;
                    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    out.close();
                    //保存图片后发送广播通知更新数据库
                    Uri uri = Uri.fromFile(file);
                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected void paused(BaseDownloadTask task, int soFarBytes, int totalBytes) {
                System.out.println("paused:");
            }

            @Override
            protected void error(BaseDownloadTask task, Throwable e) {
                System.out.println("error:");
            }

            @Override
            protected void warn(BaseDownloadTask task) {
                System.out.println("warn:");
            }
        }).start();

}