一、问题现象
在开发aar中有一个保存图片的功能、代码如下,但是在demo中保存是没有问题的,给宿主APP集成以后再调用这段代码就会报no such file相关异常、权限相关已经获取,bitmap也不为空,
public static String saveImageToGallery(Bitmap bmp, String bitName) { if (bmp == null) { Log.e("bingo", "bitmap is null"); return ""; } // 首先保存图片 File appDir = new File(Environment.getExternalStorageDirectory(), "emFace"); if (!appDir.exists()) { appDir.mkdir(); } File file = new File(appDir, bitName + ".jpeg"); try { file.createNewFile(); FileOutputStream fos = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 30, fos); fos.flush(); fos.close(); Log.e("bingo", "保存成功"); return file.getAbsolutePath(); } catch (FileNotFoundException e) { Log.e("bingo", e.getMessage()); e.printStackTrace(); } catch (IOException e) { Log.e("bingo", e.getMessage()); e.printStackTrace(); } return ""; // setPhotoFile(file); }
二、问题分析
先后验证了权限请求、bitmap是否为空、目录的创建、地址的字符串、发现都没有问题。
三、问题解决
后来换了一种思路去修改build.gralde的配置。发现我的aar targetsdkVersion和我集成的APP中的targetSdkversion版本不一致。统一后问题修复。
aar项目的的项目配置
compileSdkVersion 28 buildToolsVersion '28.0.2' defaultConfig { minSdkVersion 19 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }
存在问题时APP的项目配置如下
compileSdkVersion 29 buildToolsVersion '29.0.2' defaultConfig { minSdkVersion 19 targetSdkVersion 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }
修改后配置,并且问题修复
compileSdkVersion 28 buildToolsVersion '28.0.2' defaultConfig { minSdkVersion 19 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" }
如果你也遇到了以上问题、并且这种方式解决了你的异常,还请点个赞吧~只看不赞,纯属扯淡~