Android 存imageview图片到相册出错原因?请高手指点!

whjman 2020-11-24 05:21:27
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)));
}
...全文
4329 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
whjman 2020-11-25
  • 打赏
  • 举报
回复
问题已解决。感谢 魔仙女、jing、无双的回复!
总结最重要的几点:
1、AndroidManifest.xml 中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
2、AndroidManifest.xml 中
android:requestLegacyExternalStorage="true"
3、MainActivity.java 中 动态获取权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
}
Jing丶無雙 2020-11-25
  • 打赏
  • 举报
回复
在file = new File(fileName);这行代码后创建文件试试
			// bitName是文件具体文件名吧,应该带了.png.jpg类似这样的图片类型文件 名后缀
			file = new File(fileName);
			if (file.exists()) {// 存在文件
				file.delete();
				file.createNewFile();
			} else {// 不存在文件
				file.createNewFile();
			}
魔仙女 2020-11-25
  • 打赏
  • 举报
回复
在application添加android:requestLegacyExternalStorage="true"如图所示
whjman 2020-11-24
  • 打赏
  • 举报
回复
APP在调试中,存imageView图片到手机相册,报 FileNotFoundException 异常;APP中存储读写权限已打开,执行到“FileOutputStream out=new FileOutputStream(file);”报 FileNotFoundException 异常。 机型:安卓智能手机,系统版本:Android 10,编程环境:Android Studio 4.1
BugFree_张瑞 2020-11-24
  • 打赏
  • 举报
回复
建议提问题: 1、报什么错? 2、机型、系统版本

80,352

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧