Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Taking screenshot and showing image into gallery

Posted on November 14, 2022

Solution 1 :

This line solve my answer

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

Copied from Here:

android – save image into gallery

Problem :

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) {
           
        }
    }

Comments

Comment posted by blackapps

getExternalFilesDir()

READ  [ANSWERED] javascript - Android: how to run a local server in webview? ( = emulate a server in webview)
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress