Android aar中保存文件no such file or directory的问题解决

一、问题现象

在开发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"
}

如果你也遇到了以上问题、并且这种方式解决了你的异常,还请点个赞吧~只看不赞,纯属扯淡~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FrancisBingo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值