Android保存图片到系统图库

最近有些用户反映保存图片以后在系统图库找不到保存的图片,遂决定完全查看并解决下。数据库

Adnroid中保存图片的方法可能有以下两种:app

  • 第一种是本身写方法,以下代码:code

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/下,文件名以当前系统时间命名,可是这种方法保存的图片没有加入到系统图库中orm

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

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

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

看似上述第二种方法就是咱们要用到的方法,可是惋惜的调用上述第二种插入图库的方法图片并无马上显示在图库中,而咱们须要马上更新系统图库以便让用户能够马上查看到这张图片。ip

  • 更新系统图库的方法get

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

上面那条广播是扫描整个sd卡的广播,若是你sd卡里面东西不少会扫描好久,在扫描当中咱们是不能访问sd卡,因此这样子用户体现很很差,因此下面咱们还有以下的方法:it

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

或者还有以下方法:io

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))); }
相关文章
相关标签/搜索