簡體   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