繁体   English   中英

如何使用intent分享当前页面的屏幕截图?

[英]How to share Screenshot of current page by using intent?

我正在开发和Android博客应用程序,我想分享我当前的博客页面screenshot.I尝试,但它显示文件格式不支持..请帮我找到我的错误..谢谢你提前

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

这是允许我的屏幕截图存储在SD卡上的代码,以后用于满足您的任何需求:

首先,您需要添加适当的权限来保存文件:

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

这是代码(在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();
    }
}

这就是你打开最近生成的图像的方法:

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

如果要在片段视图上使用它,请使用:

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

代替

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

on takeScreenshot()函数

注意:

如果对话框包含曲面视图,则此解决方案不起作用。 有关详细信息,请检查以下问题的答案:

Android截图表面视图显示黑屏

如果您有任何疑问,请通过此链接

您可以在布局中获取屏幕视图的Bitmap view将是您的布局viewLinearRelativeLayout

view.setDrawingCacheEnabled(true);

view.buildDrawingCache();

Bitmap returnedBitmap = view.getDrawingCache();

然后必须转换为byte[]以便您可以像下面这样发送Intent

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

并使用Intent发送数据,如下所示:

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

在你的SecondActivity上得到Intent ,如下所示:

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

暂无
暂无

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

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