第一步:先申請(qǐng)讀寫權(quán)限(讀寫權(quán)限都要申請(qǐng))
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) {
//進(jìn)行權(quán)限請(qǐng)求
ActivityCompat.requestPermissions(WebViewActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, SAVE_BITMAP);
} else {
saveImageToGallery(bmp);
}
} else {
saveImageToGallery(bmp);
}
/**
* 圖片保存相冊(cè)方法
*
* @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();
}
// //其次把文件插入到系統(tǒng)圖庫(kù)
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();
}
}
//更新圖庫(kù)
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);
}
//得到絕對(duì)地址
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語(yǔ)法
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()
}
//其次把文件插入到系統(tǒng)圖庫(kù)
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()
}
}
//更新圖庫(kù)
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)
}
//得到絕對(duì)地址
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