gpt4 book ai didi

java - Android:拍摄 View 的屏幕截图,保存图像并检索它

转载 作者:行者123 更新时间:2023-11-30 10:47:12 27 4
gpt4 key购买 nike

我基本上是在尝试在我的 Activity 中捕获相对布局的屏幕截图并将其保存为 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;
}

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

最佳答案

  1. 先给相关 View 分配一个id

  2. 使用 findViewById() 获取引用然后按照这个代码 fragment

//用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"/>

希望这有帮助:)

关于java - Android:拍摄 View 的屏幕截图,保存图像并检索它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36223942/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com