Android(安卓)保存图片到本地并且微信能识别出来
16lz
2021-01-26
Android 图片保存到本地,更新相册,并且打开微信发送图片的时候,可以被识别
测了一下,大部分手机都可以适配,不适配的再找别的吧
public static void saveImageToGallery(Bitmap mBitmap) { if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return; } // 首先保存图片 File appDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile(); if (!appDir.exists()) { appDir.mkdir(); } String fileName = System.currentTimeMillis() + ".jpg"; File file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return; } catch (IOException e) { e.printStackTrace(); return; } //把文件插入到系统图库 保存的路径为 Environment.DIRECTORY_DCIM 和 下方的 Uri.fromFile(file) 一起用 会造成 部分手机(华为P20 ,OPPO A83 ,美图手机等) 插入两张相同的图片,一张为当前时间 一张为 1970.01.01 时间的; // Google Pixel 的手机 表示 没保存成功,保存之后 找不到,不作处理了,后期再单独优化。// try {// MediaStore.Images.Media.insertImage(getApplication().getContentResolver(), file.getAbsolutePath(), fileName, null);// } catch (FileNotFoundException e) {// e.printStackTrace();// } // 注释掉上方插入图库代码, 只去通知更新当前文件, 不影响 微信等正常读取图片 // 最后通知图库更新 Uri.parse("file://" + "") 更新之后,微信识别不出来, // Uri.fromFile(file) 可以被微信识别出来// getApplication().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + ""))); getApplication().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file))); }
更多相关文章
- android下图片处理方法[转]
- android 循环播放图片实现
- 【Android开发经验】FaceBook推出的Android图片加载库-Fresco
- android 编译facebook/fresco图片库
- Android(安卓)MaskedImage
- [android UI]自定义各种UI,收集在此,以备后用!
- Android培训班(43)
- 《第一行代码Android》学习总结第八章 使用摄像头与相册
- android-TabHost标签控件的图片和文字不能同时显示解决方案