简体   繁体   English

截屏并将图像显示到图库中

[英]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()); MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, file.getName() ,file.getName());

Copied from Here:从这里复制:

android - save image into gallery android - 将图像保存到图库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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