首页 > android保存图片到本地相册

android保存图片到本地相册

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

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

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


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

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文件夹的。因为这不科学。


直接放到相册里~

【热门文章】
【热门文章】