繁体   English   中英

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

[英]Taking screenshot and showing image into 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) {
           
        }
    }

这条线解决了我的答案

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

从这里复制:

android - 将图像保存到图库

暂无
暂无

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

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