无法将图像添加到图库

Can not add image to the gallery

我正在处理 jpeg 图像。现在我在我的应用程序 directory(data/data/'pakage'/'image_name') 中保存了一张图片,需要与图库文件夹共享。此代码工作正常:

 WallpaperItem item = getWallpaperItem(source, loadDateStr.isEmpty() ?
     history.get(history.size() - 1) : loadDateStr);       
 MediaStore.Images.Media.insertImage(context.getContentResolver(), item.getImage(),
     String.format("%s %s", source.name(), loadDateStr ), item.getDescription()); 

'source' 和 'loadDateStr' 是方法参数。

但是这样我只能将图像插入 'Pictures' 相册。我试图在单独的专辑中展示它。这是我试图做的:

        loadDateStr = loadDateStr.isEmpty() ? history.get(history.size() - 1) : loadDateStr;
        String fileName = source.name() + loadDateStr + ".jpg";
        File file = new File(dataDirectoryPath, fileName);
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
        context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

日志:

 12-24 12:33:35.862 28695/'pakage' I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum =0
12-24 12:33:35.862 28695-28782/'pakage' I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1

12-24 12:33:35.862 28695-28782/'pakage' I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3

制作专辑(screenshot), but image is empty(screenshot)。

            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            loadDateStr = loadDateStr.isEmpty() ? history.get(history.size() - 1) : loadDateStr;
            String fileName = source.name() + loadDateStr + ".jpg";
            File file = new File(dataDirectoryPath, fileName);
            if(file.exists()){
                Uri contentUri = Uri.fromFile(file);
                mediaScanIntent.setData(contentUri);
                context.sendBroadcast(mediaScanIntent);
            }

没有任何效果。 日志是一样的。

        loadDateStr = loadDateStr.isEmpty() ? history.get(history.size() - 1) : loadDateStr;
        String fileName = source.name() + loadDateStr + ".jpg";
        File file = new File(dataDirectoryPath, fileName);
            if (file.exists())
                MediaScannerConnection.scanFile(context, new String[]{ file.getAbsolutePath() },
                        new String[]{ "image/jpeg"}, new MediaScannerConnection.MediaScannerConnectionClient() {
                            @Override
                            public void onMediaScannerConnected() {

                            }

                            @Override
                            public void onScanCompleted(String path, Uri uri) {

                            }
                        });

没有任何效果。 日志是一样的。

有人可以帮我吗?

我的解决方案:

    String fileName = source.name() + loadDateStr + ".jpg"; //My image file name.
    File dir = Environment.getExternalStoragePublicDirectory('Directory name');
    dir.mkdirs();
    File imageFile = new File(dir, fileName);
    imageFile.createNewFile();

    OutputStream output = new FileOutputStream(imageFile.getPath());
    getWallpaperItem(source, loadDateStr).getImage().compress(Bitmap.CompressFormat.JPEG, 85, output);

    output.flush();
    output.close();

    MediaScannerConnection.scanFile(context, new String[]{ imageFile.getAbsolutePath()}, null, null);

Result screenshot