移动app - android保存图片到本地相册

 我没资格我不配 发布于 2022-11-01 03:50

APP中集成了环信得聊天功能,现在需要将聊天中别人发给我的图片保存到本地。请问如何能直接显示在相册中,让用户已下载就可见,让微信发送图片时也能直接找到。

考虑到应该各种机型的情况不同,自己试了下,在魅族上图片保存到DCIM下,魅族的相册能直接看到,但在小米、moto x pro等机型上就不行了。
有尝试过使用广播,但那广播貌似需要一定api之下才能使用。也使用过MediaScanner等扫描的办法。

请问有什么较好的解决办法嘛?让app下载的图片能直接显示在相册中。
谢谢。

3 个回答
  • 直接放到相册里~

    2022-11-01 08:50 回答
  • 放到图片目录下啊。。
    Picture 这个目录
    在下面建立一个你的目录随意叫什么 (和Screenshots 截图目录同级别)
    即可。

    一般DICM里面是保存拍照的,你需要建立和Camera同级别的目录,否则有可能不会列入某些选择器列表。
    同时,也是不推荐把网络图片保存到DICM文件夹的。因为这不科学。

    2022-11-01 08:59 回答
  • 你这个问题我也遇过,试一下我下面给出的方法。小米,魅簇都可以...

    public static void saveImageToGallery(Context context, Bitmap bmp) {
        if (bmp == null){
            ToastUtils.show(context, "保存出错了...");
            return;
        }
        // 首先保存图片
        File appDir = new File(BaseApplication.app.getTmpDir(), "ywq");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            ToastUtils.show(context, "文件未发现");
            e.printStackTrace();
        } catch (IOException e) {
            ToastUtils.show(context, "保存出错了...");
            e.printStackTrace();
        }catch (Exception e){
            ToastUtils.show(context, "保存出错了...");
            e.printStackTrace();
        }
    
        // 最后通知图库更新
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        context.sendBroadcast(intent);
        ToastUtils.show(context, "保存成功了...");
    }
    2022-11-01 09:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有