如何将 android 中的高质量位图图像插入图库

How to insert high quality Bitmap image in android into Gallery

我知道插入方法

  MediaStore.Images.Media.insertImage(..............)

插入原始位图图像的缩略图,我需要一种方法来保存位图而不压缩以保持其像素原样(隐写术),我需要将图像存储在内部存储库中。

图库可以包含 android 上应用程序的文件夹,要获得高分辨率文件,需要将它们存储在图库外,并告诉图库您的文件和应用程序文件夹,并将您的文件显示为缩略图,所以我实现了这个方法来执行我需要的,我希望能帮助别人

  private void SaveImage(Bitmap segg) {

    OutputStream fOut = null;
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String fileName = "Image-"+ n +".png";
    final String appDirectoryName = "TBStego";
    final File imageRoot = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), appDirectoryName);

    imageRoot.mkdirs();
    final File file = new File(imageRoot, fileName);
    try {
        fOut = new FileOutputStream(file);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    segg.compress(Bitmap.CompressFormat.PNG, 100, fOut);
    try {
        Toast.makeText(ExtractActivity.this,
                file.getAbsolutePath(),
                Toast.LENGTH_LONG).show();
        fOut.flush();
        fOut.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    ContentValues values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE,"stego");
    values.put(MediaStore.Images.Media.DESCRIPTION, "stego description");
    values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(MediaStore.Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
    values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
    values.put("_data", file.getAbsolutePath());
    Toast.makeText(ExtractActivity.this,
            file.getAbsolutePath(),
            Toast.LENGTH_LONG).show();
    ContentResolver cr = getContentResolver();
    cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Toast.makeText(ExtractActivity.this, "The Image thumbnail created in Gallery ", Toast.LENGTH_LONG).show();
}