public void saveBitmap(ImageView imageView, String bitName){
String fileName ;
File file ;
BitmapDrawable bmpDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bmpDrawable.getBitmap();
if (bitmap==null){
return;
}
if(Build.BRAND .equals("Xiaomi") ){ // 小米手機
fileName = Environment.getExternalStorageDirectory().getPath()+"/DCIM/Camera/"+bitName ;
}else{ // Meizu 、Oppo
fileName = Environment.getExternalStorageDirectory().getPath()+"/DCIM/"+bitName ;
}
file = new File(fileName);
Toast.makeText(mContext, fileName, Toast.LENGTH_LONG).show();
try{
FileOutputStream out=new FileOutputStream(file);
// 格式為 JPEG,照相機拍出的圖片為JPEG格式的,PNG格式的不能顯示在相冊中
if(bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out))
{
out.flush();
out.close();
// 插入圖庫
MediaStore.Images.Media.insertImage (getContext ().getContentResolver (), file.getAbsolutePath(), bitName, null);
}
}
catch (FileNotFoundException e) { Toast.makeText(mContext, "FileNotFoundException", Toast.LENGTH_LONG).show(); e.printStackTrace(); }
catch (IOException e) { Toast.makeText(mContext, "IOException", Toast.LENGTH_LONG).show();e.printStackTrace(); }
// 發送廣播,通知重繪圖庫的顯示
this.getActivity ().sendBroadcast(new Intent (Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + fileName)));
}
uj5u.com熱心網友回復:
建議提問題:1、報什么錯?
2、機型、系統版本
uj5u.com熱心網友回復:
APP在除錯中,存imageView圖片到手機相冊,報 FileNotFoundException 例外;APP中存盤讀寫權限已打開,執行到“FileOutputStream out=new FileOutputStream(file);”報 FileNotFoundException 例外。機型:安卓智能手機,系統版本:Android 10,編程環境:Android Studio 4.1
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/227397.html
標籤:Android
上一篇:記憶體溢位
