Android 存imageview图片到相册出错原因?请高手指点!
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)));
}