android displays downloaded pictures to media albums

     * @param context
     * @param bitmap
     * @param type 0 png 1 jpeg
    private void saveImageToGallery(Context context, Bitmap bitmap ,int type) {
        // Create screenshot directory if it doesn't exist
        String dirName = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "MImages";//MImages is the album name, which can be changed by yourself. Be careful not to duplicate the album name of the system
        File fileDir = new File(dirName);
        if (!fileDir.exists()) {
        long mImageTime = System.currentTimeMillis();//Take the current system time
        // media provider uses seconds for DATE_MODIFIED and DATE_ADDED, but milliseconds
        // for DATE_TAKEN
        long dateSeconds = mImageTime / 1000;
        String mImageFileName = dateSeconds + (type == 1?".jpeg": ".png"); //Named after save time
        String mImageFilePath = dirName + File.separator + mImageFileName; //Note that the mImageFilePath here is: directory name + file name
        int mImageWidth = bitmap.getWidth();
        int mImageHeight = bitmap.getHeight();

        // Save the screenshot to the MediaStore
        ContentValues values = new ContentValues();
        ContentResolver resolver = context.getContentResolver();
        values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath);
        values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName);
        values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName);
        values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime);
        values.put(MediaStore.Images.ImageColumns.DATE_ADDED, dateSeconds);
        values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, dateSeconds);
        values.put(MediaStore.Images.ImageColumns.MIME_TYPE, (type == 1? "image/jpeg" : "image/png"));
        values.put(MediaStore.Images.ImageColumns.WIDTH, mImageWidth);
        values.put(MediaStore.Images.ImageColumns.HEIGHT, mImageHeight);
        Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        try {
            OutputStream out = resolver.openOutputStream(uri);
            bitmap.compress((type == 0?Bitmap.CompressFormat.PNG:Bitmap.CompressFormat.JPEG), 100, out);
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        // update file size in the database
        values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
        resolver.update(uri, values, null, null);

This method is modified by referring to the screenshot source code of android system. In the past, we have seen that the way others write is to save the pictures to the local place first and then refresh the media library through broadcasting. This way can not meet our needs very well. And with the upgrading of android system, this way is gradually blocked by the official. Also, students must use Eviroment when obtaining the local file path, otherwise unexpected problems may occur.
Some students will ask again, the system does not provide a method MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description"); yes, but this method does not allow you to display the picture to the album immediately. You need to refresh the album, but refreshing the album is not necessarily successful. So it's not needed.

Tags: Android Database

Posted on Sun, 03 May 2020 09:39:18 -0400 by Shadow Wolf