Android中怎么保存图片到系统图库

发布时间:2021-07-22 16:52:06 作者:Leah
来源:亿速云 阅读:129

这篇文章将为大家详细讲解有关Android中怎么保存图片到系统图库,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

第一种是自己写方法,如下代码:

public static File saveImage(Bitmap bmp) {
  File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
  if (!appDir.exists()) {
    appDir.mkdir();
  }
  String fileName = System.currentTimeMillis() + ".jpg";
  File file = new File(appDir, fileName);
  try {
    FileOutputStream fos = new FileOutputStream(file);
    bmp.compress(CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
}

以上代码便是将Bitmap保存图片到指定的路径/sdcard/Boohee/下,文件名以当前系统时间命名,但是这种方法保存的图片没有加入到系统图库中

第二种是调用系统提供的插入图库的方法:

复制代码 代码如下:

MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");

调用以上系统自带的方法会把bitmap对象保存到系统图库中,但是这种方法无法指定保存的路径和名称,上述方法的title、description参数只是插入数据库中的字段,真实的图片名称系统会自动分配。

看似上述第二种方法就是我们要用到的方法,但是可惜的调用上述第二种插入图库的方法图片并没有立刻显示在图库中,而我们需要立刻更新系统图库以便让用户可以立刻查看到这张图片。

更新系统图库的方法

复制代码 代码如下:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

上面那条广播是扫描整个sd卡的广播,如果你sd卡里面东西很多会扫描很久,在扫描当中我们是不能访问sd卡,所以这样子用户体现很不好,所以下面我们还有如下的方法:

复制代码 代码如下:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/Boohee/image.jpg"))););


或者还有如下方法:

final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() {
  public void onMediaScannerConnected() {
    msc.scanFile("/sdcard/Boohee/image.jpg", "image/jpeg");
  }
  public void onScanCompleted(String path, Uri uri) {
    Log.v(TAG, "scan completed");
    msc.disconnect();
  }
});

上面代码的图片路径不管是通过自己写方法还是系统插入图库的方法都可以很容易的获取到。

终极完美解决方案

那么到这里可能有人又会问了,如果我想把图片保存到指定的文件夹,同时又需要图片出现在图库里呢?答案是可以的,sdk还提供了这样一个方法:

复制代码 代码如下:

MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description");

上述方法的第二个参数是image path,这样的话就有思路了,首先自己写方法把图片指定到指定的文件夹,然后调用上述方法把刚保存的图片路径传入进去,最后通知图库更新。

所以写了一个方法,完整的代码如下:

public static void saveImageToGallery(Context context, Bitmap bmp) {
  // 首先保存图片
  File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
  if (!appDir.exists()) {
    appDir.mkdir();
  }
  String fileName = System.currentTimeMillis() + ".jpg";
  File file = new File(appDir, fileName);
  try {
    FileOutputStream fos = new FileOutputStream(file);
    bmp.compress(CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
 }
  // 其次把文件插入到系统图库
  try {
    MediaStore.Images.Media.insertImage(context.getContentResolver(),
  file.getAbsolutePath(), fileName, null);
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }
  // 最后通知图库更新
  context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
}

关于Android中怎么保存图片到系统图库就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. Python绘图库—matplotlib
  2. 微信小程序如何设置保存图片到相册权限

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:Java中怎么利用双链表互相交换任意两个节点

下一篇:Java中的map如何使用

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》