第一步:先申请读写权限(读写权限都要申请)
String base64 = (String) msg.obj;
bmp = ImageUtils.base64ToBitmap(base64);
if (Build.VERSION.SDK_INT >= M) {
if (ContextCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager
.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager
.PERMISSION_GRANTED) {
//进行权限请求
ActivityCompat.requestPermissions(WebViewActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, SAVE_BITMAP);
} else {
saveImageToGallery(bmp);
}
} else {
saveImageToGallery(bmp);
}
/**
* 图片保存相册方法
*
* @param bitmap
*/
public void saveImageToGallery(Bitmap bitmap) {
// 首先保存图片
File file = null;
String fileName = System.currentTimeMillis() + ".jpg";
File root = new File(getExternalCacheDir(), getPackageName());
if (!root.exists()) {
root.mkdir();
}
file = new File(root, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// //其次把文件插入到系统图库
try {
String insertImage = MediaStore.Images.Media.insertImage(this.getContentResolver(), file.getAbsolutePath(), fileName, null);
File file1 = new File(getRealPathFromURI(Uri.parse(insertImage),WebViewActivity.this));
updatePhotoMedia(file1,this);
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
//更新图库
private void updatePhotoMedia(File file ,Context context){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
context.sendBroadcast(intent);
}
//得到绝对地址
private static String getRealPathFromURI(Uri contentUri,Context context) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String fileStr = cursor.getString(column_index);
cursor.close();
return fileStr;
}
//kotlin语法
fun saveImageToGallery(bitmap: Bitmap) {
// 首先保存图片
var file: File? = null
val fileName = (System.currentTimeMillis()).toString() + ".jpg"
val root = File(externalCacheDir, packageName)
if (!root.exists()) {
root.mkdir()
}
file = File(root, fileName)
try {
val fos = FileOutputStream(file!!)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos)
fos.flush()
fos.close()
} catch (e: FileNotFoundException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
}
//其次把文件插入到系统图库
try {
val insertImage = MediaStore.Images.Media.insertImage(this.contentResolver, file!!.absolutePath, fileName, null)
val file1 = File(getRealPathFromURI(Uri.parse(insertImage), this))
updatePhotoMedia(file1,this);
} catch (e: FileNotFoundException) {
e.printStackTrace()
}
}
//更新图库
fun updatePhotoMedia(file: File, context: Context) {
val intent = Intent()
intent.action = Intent.ACTION_MEDIA_SCANNER_SCAN_FILE
intent.data = Uri.fromFile(file)
context.sendBroadcast(intent)
}
//得到绝对地址
fun getRealPathFromURI(contentUri: Uri, context: Context): String {
val proj = arrayOf(MediaStore.Images.Media.DATA)
val cursor = context.getContentResolver().query(contentUri, proj, null, null, null)
val column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
cursor.moveToFirst()
val fileStr = cursor.getString(column_index)
cursor.close()
return fileStr
}
okay