移动app - android保存图片到本地相册
阿神
阿神 2017-04-17 15:39:45
[Android讨论组]

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

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

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

阿神
阿神

闭关修行中......

全部回复(3)
黄舟

你这个问题我也遇过,试一下我下面给出的方法。小米,魅簇都可以...

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, "保存成功了...");
}
黄舟

放到图片目录下啊。。
Picture 这个目录
在下面建立一个你的目录随意叫什么 (和Screenshots 截图目录同级别)
即可。

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

迷茫

直接放到相册里~

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号