Android saves pictures to system albums

Posted by ofi on Wed, 22 May 2019 01:12:35 +0200

github address: (full demo, welcome to download)
https://github.com/zhouxu88/SaveImgToGallery/tree/master

There may be two ways to save images in Adnroid:

  • The first method is to call the insertion library provided by the system.
    MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");
    Calling the method that comes with the above system will save the bitmap object to the system gallery, but this method can not specify the path and name to save. The title and description parameters of the above method are only inserted into the fields in the database, and the real image name system will automatically allocate.

perhaps

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

However, it was found that the saved pictures could not be viewed in the album, and it was found that the system Gallery needed to be refreshed.

  • Method of Updating System Graphic Library
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
    The above broadcasting is to scan the whole sd card broadcasting. If many of the contents of your sd card will be scanned for a long time, we can't access the sd card during the scanning, so the user's performance is not good, so we still have the following methods:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your path"))););

Complete code

  • 1. Save Bitmap to a locally specified path
  • 2. Notify system album gallery to refresh data by broadcasting
public class ImgUtils {
    //Save the file to the specified path
    public static boolean saveImageToGallery(Context context, Bitmap bmp) {
        // First save the picture
        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "dearxy";
        File appDir = new File(storePath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //Compress and save pictures by io stream
            boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
            fos.flush();
            fos.close();

            //Insert files into the system Gallery
            //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

            //Update the database by sending broadcast notifications after saving pictures
            Uri uri = Uri.fromFile(file);
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (isSuccess) {
                return true;
            } else {
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }
}

Be careful:
1. Don't forget permissions

  <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

2. If it is a 6.0 mobile phone, remember to apply for permission first, get permission, then save, otherwise it will fail.



Author: accompany you to chat
Link: http://www.jianshu.com/p/8cede074ba5b
Source: Brief Book
Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

Topics: Android github Database Mobile