简体   繁体   中英

Taking screenshot and showing image into gallery

I'm taking a screenshot of a dialog and trying to show the image into the gallery. I've saved the image into the external storage but the image is not visible into the gallery. When I go the storage location I can see the latest image there but the image is not showing into the gallery and I've tried multiple codes for this purpose but none work and there is no error as well. Can someone help me with this issue?

Below is the code I'm using to take a screenshot and trying to refresh the gallery:

    public void showAlertDialog(final Activity activity) {
        Dialog dialog = new Dialog(activity);
        currentDialog = dialog;
        currentActivity = activity;
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.payment_transaction_layout);

        ImageView imageViewSS = dialog.findViewById(R.id.imageView19);
   
        imageViewSS.setOnClickListener(v -> {
    
            checkExternalStoragePermission(dialog);
        });

 
        dialog.show();

    }

    private void checkExternalStoragePermission(Dialog dialog) {
        if (ContextCompat.checkSelfPermission(currentActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(currentActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_EXTERNAL_STORAGE);
        } else {
            Bitmap bitmap = takeScreenShot(dialog);
            saveBitmap(bitmap);
        }
    }

    private static Bitmap takeScreenShot(Dialog dialog) {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

        // create bitmap screen capture
        View v1 = dialog.getWindow().getDecorView().getRootView();

        v1.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
        v1.setDrawingCacheEnabled(false);
        return bitmap;


    }

    public void saveBitmap(Bitmap bitmap) {
        File imagePath = currentActivity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File file = null;
        try {
            /**
             *Creating file with the extension file name and given file name
             * */
            file = File.createTempFile(timeStamp, ".png", imagePath);
            Log.e("location", "" + file.getAbsolutePath());
            Log.e("TransferClass", "Clicked: " + fromWhere);
            saveImageToGallery(bitmap, file);
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("TransferClass", "Exception caught: " + e.getMessage());
        }

    }

    private void saveImageToGallery(Bitmap bitmap, File file) {
        FileOutputStream fos;
        try {
            path = file.getAbsolutePath();
            fos = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            Log.e("Screenshot", "saved successfully" + " Path " + path);

            fos.flush();
            fos.close();
            //code for showing the image into gallery
            currentActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(path)));
           
        } catch (IOException e) {
           
        }
    }

This line solve my answer

MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, file.getName() ,file.getName());

Copied from Here:

android - save image into gallery

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM