识别图片格式
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mFile.getAbsolutePath(), options);
String type = options.outMimeType;
if (TextUtils.isEmpty(type)) {
type = "未能识别的图片";
} else {
type = type.substring(6, type.length());
}
System.out.println("文件格式是:" + type);
将图片插入到系统相册
MediaStore.Images.Media.insertImage(mContext.getContentResolver(), mFile.getAbsolutePath(), System.currentTimeMillis() + "." + type, null);
更新相册
Uri uri = Uri.fromFile(mFile);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
mContext.sendBroadcast(intent);
采用glide下载图片
Glide.with(mContext).load(imgUrl).downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get();
如下
new Thread(new Runnable() { @Override public void run() { File mFile = null; try { mFile = Glide.with(mContext).load(imgUrl).downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL).get(); if (mFile != null && mContext != null) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(mFile.getAbsolutePath(), options); String type = options.outMimeType; if (TextUtils.isEmpty(type)) { type = "未能识别的图片"; } else { type = type.substring(6, type.length()); } System.out.println("文件格式是:" + type); MediaStore.Images.Media.insertImage(mContext.getContentResolver(), mFile.getAbsolutePath(), System.currentTimeMillis() + "." + type, null); //更新相册 Uri uri = Uri.fromFile(mFile); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri); mContext.sendBroadcast(intent); } } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } } }).start();
第二种方法--通过系统下载图片
private void downloadImageBySystem(String dowloadPath) { final DownloadManager dManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE); Uri uri = Uri.parse(dowloadPath); DownloadManager.Request request = new DownloadManager.Request(uri); // 设置下载路径和文件名 File file = new File(saveImagePath); //如果file不存在,就创建这个file if (!file.exists()) { file.mkdir(); } request.setDestinationInExternalPublicDir(saveImagePath, dowloadPath.substring(dowloadPath.lastIndexOf("/")+1, dowloadPath.length())); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setMimeType("application/vnd.android.package-archive"); // 设置为可被媒体扫描器找到 request.allowScanningByMediaScanner(); // 设置为可见和可管理 request.setVisibleInDownloadsUi(true); // 获取此次下载的ID final long refernece = dManager.enqueue(request); // 注册广播接收器,当下载完成时自动安装 IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); BroadcastReceiver receiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { long myDwonloadID = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); if (refernece == myDwonloadID) { LogUtils.e("下载完成"); } } }; mContext.registerReceiver(receiver, filter); }
这种方式最简单