简体   繁体   中英

How to share Screenshot of current page by using intent?

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

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();
    }
}

And this is how you can open the recently generated image:

private void openScreenshot(File imageFile) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    Uri uri = Uri.fromFile(imageFile);
    intent.setDataAndType(uri, "image/*");
    startActivity(intent);
}

If you want to use this on fragment view then use:

View v1 = getActivity().getWindow().getDecorView().getRootView();

instead of

View v1 = getWindow().getDecorView().getRootView();

on takeScreenshot() function

Note:

This solution doesn't work if your dialog contains a surface view. For details please check the answer to the following question:

Android Take Screenshot of Surface View Shows Black Screen

if u have any doubt please go through this link

You can get the Bitmap of your screen view inside your layouts. Where view will be your layout view s like Linear or RelativeLayout

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap returnedBitmap = view.getDrawingCache();

then have to convert into byte[] so that you can send with the Intent like this following:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
returnedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

and send data with Intent like this:

Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("bitmap_",byteArray);

get Intent on your SecondActivity like this:

byte[] byteArray = getIntent().getByteArrayExtra("bitmap_");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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