繁体   English   中英

Android:截取视图的截图,保存图像并检索它

[英]Android: Taking screenshot of a View, Saving the image and retrieving it

我基本上试图在我的活动中捕获相对布局的屏幕截图并将其保存为 PNG 图像。 然后我需要将它作为位图文件检索。 我写了一些代码来完成这项工作,但遗憾的是屏幕截图没有保存在我的设备上。

XML

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Other views -->

</RelativeLayout>

代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_view);

        //The view that needs to be captured as an image
        parent = (RelativeLayout) findViewById(R.id.parent);

        Bitmap image = getBitmapFromView(parent);
}

public static Bitmap getBitmapFromView(View view) {
        Date now = new Date();
        android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
        Bitmap returnBitmap = null;

        try {

            String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";

            view.setDrawingCacheEnabled(true);
            Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
            view.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();

            //Retrieve the image as a Bitmap and return it
            if(imageFile.exists()){
                returnBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
            }

        } catch (Throwable e) {
            // Several error may come out with file handling or OOM
            e.printStackTrace();
        }
        return returnBitmap;
    }

上面的代码不起作用。 没有错误,但未捕获和保存视图。 有人可以指出问题或给我一个更好的解决方案吗?

  1. 首先为相关视图分配一个id

  2. 使用findViewById()获取引用然后按照此代码段进行操作

//定义一个带有View的高度和宽度的位图

Bitmap viewBitmap = Bitmap.createBitmap(v.getWidth(),v.getHeight(),Bitmap.Config.RGB_565);

Canvas viewCanvas = new Canvas(viewBitmap);

//get background of canvas
Drawable backgroundDrawable = v.getBackground();

if(backgroundDrawable!=null){
backgroundDrawable.draw(viewCanvas);//draw the background on canvas;
}
else{
viewCanvas.drawColor(Color.GREEN);
//draw on canvas
v.draw(viewCanvas) 
}

//write the above generated bitmap  to a file
String fileStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        OutputStream outputStream = null;
        try{
            imgFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),fileStamp+".png");
            outputStream = new FileOutputStream(imgFile);
            b.compress(Bitmap.CompressFormat.PNG,40,outputStream);
            outputStream.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }

用 JPEG 替换 PNG

不要忘记添加此权限

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

希望这可以帮助 :)

暂无
暂无

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

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