How to share Screenshot of current page by using intent?

General Tech Bugs & Fixes 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Bugs & Fixes related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

 

I am developing and android blog application where I want to share my current blog page screenshot.I try but it showing file format is not supported..please help me to find my error..Thanks in advance

MyAdapter.java

        sendImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bitmap app_snap = ((BitmapDrawable)movie_image.getDrawable()).getBitmap();
                String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SaveImg";
                System.out.println("****FILEPATH **** : " + file_path);
                File imagePath = new File(Environment.getExternalStorageDirectory() + "/scr.png");
                FileOutputStream fos;
                try {
                    fos = new FileOutputStream(imagePath);
                    System.out.println("****FILEPATH1 **** : " + file_path);
                    app_snap.compress(Bitmap.CompressFormat.PNG, 200, fos);
                    System.out.println("****FILEPATH2 **** : " + file_path);
                    fos.flush();
                    fos.close();
                }
                catch (IOException e) {
                    System.out.println("GREC****** "+ e.getMessage());
                }
                Intent sharingIntent = new Intent();
                Uri imageUri = Uri.parse(imagePath.getAbsolutePath());
                sharingIntent.setAction(Intent.ACTION_SEND);
                sharingIntent.setType("image/png");
                sharingIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                context.startActivity(sharingIntent);
            }
        });
profilepic.png
manpreet 2 years ago

 

Here is the code that allowed my screenshot to be stored on an SD card and used later for whatever your needs are:

First, you need to add a proper permission to save the file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

And this is the code (running in an Activity):

private void takeScreenshot() {
    Date now = new Date();
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);

    try {
        // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

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


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

        File imageFile = new File(mPath);

        FileOutputStream outputStream = new FileOutputStream(imageFile);
        int quality = 100;
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
        outputStream.flush();
        outputStream.close();

        openScreenshot(imageFile);
    } catch (Throwable e) {
        // Several error may come out with file handling or DOM
        e.printStackTrace();
    }
}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.