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