简体   繁体   English

使用C#在android中截屏

[英]Screen shot in android with c#

I am working on an android project in which I want to take the screen shot of current activity and save that as .jpeg file in my android device. 我正在开发一个Android项目,在该项目中我想获取当前活动的屏幕截图并将其另存为.jpeg文件在我的android设备中。

When I use instance of FileOutputStream to write the stream of bitmap image into a file, it gives me following error 当我使用FileOutputStream实例将位图图像流写入文件时,出现以下错误

Argument 3 cannot converted from 'Java.IO.FileOutputStream' to 'System.IO.Stream' 参数3无法从“ Java.IO.FileOutputStream”转换为“ System.IO.Stream”

My code 我的密码

 private void ShareButton_Click(object sender, EventArgs e)
    {
        //create a bitmap screen capture
        View screen = FindViewById(Resource.Layout.AboutImage);
        Bitmap bitmap = Bitmap.CreateBitmap(screen.GetDrawingCache(false));
        screen.SetWillNotCacheDrawing(true);
        image = new File(directory, "Eco_Friendly " + mc.identifier);

        FileOutputStream outputstream = new FileOutputStream(image);

        int quality = 100;
        bitmap.Compress(Bitmap.CompressFormat.Jpeg, quality, outputstream);//Here is error
    }

Q:How to solve this problem? 问:如何解决这个问题?

Try this code: 试试这个代码:

package com.screen.shots;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class CaptureScreenShots extends Activity {
    LinearLayout L1;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen_shots);
         L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
            Button but = (Button) findViewById(R.id.munchscreen);
            but.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    View v1 = L1.getRootView();
                    v1.setDrawingCacheEnabled(true);
                    Bitmap bm = v1.getDrawingCache();
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
                    image = (ImageView) findViewById(R.id.screenshots);
                    image.setBackgroundDrawable(bitmapDrawable);
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.screen_shots, menu);
        return true;
    }

}

Result: 结果:

在此处输入图片说明

like this 像这样

View root = getWindow().getDecorView().getRootView();
root.setDrawingCacheEnabled(true);
root.buildDrawingCache();
Bitmap snapshot = root.getDrawingCache();

at last, you should call this 最后,你应该叫这个

root.destroyDrawingCache();

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

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