How to insert high quality Bitmap image in android into Gallery

前提是你 提交于 2019-12-12 04:07:53

问题


I know the insert method

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

Insert a thumbnail instaed of the original Bitmap image,I need a way to save Bitmap with no compress to keep its pixels as they are (steganography), I need the image to be stored on gallery in internal storage.


回答1:


The Gallery can contains folders for application on android, to get high resolution file there is need to store them outside the gallery and tell gallery about your file and your application folder and show your files as thumbnails,so I implement this method which perform what I need and I hope help others

  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();
}


来源:https://stackoverflow.com/questions/35814674/how-to-insert-high-quality-bitmap-image-in-android-into-gallery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!