gpt4 book ai didi

java - onClick takeScreenShot 导致暂停

转载 作者:行者123 更新时间:2023-12-01 13:35:40 25 4
gpt4 key购买 nike

更新如下

应用程序继续暂停。我正在尝试通过单击按钮来获取屏幕截图。当我单击按钮时它会暂停。这是我在调试角度得到的结果:

MainActivity$1.onClick(View) line: 108  
Button(View).performClick() line: 4438
View$PerformClick.run() line: 18422
Handler.handleCallback(Message) line: 733
ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 95
Looper.loop() line: 136
ActivityThread.main(String[]) line: 5017
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 515
ZygoteInit$MethodAndArgsCaller.run() line: 779
ZygoteInit.main(String[]) line: 595
NativeStart.main(String[]) line: not available [native method]

我使用 onClick 来拍摄屏幕截图,如下所示:

            public void onClick(View view) {




Activity MainActivity = null;
//Line 108 View view1 = MainActivity.getWindow().getDecorView();
view1.setDrawingCacheEnabled(true);
view1.buildDrawingCache();
Bitmap b1 = view1.getDrawingCache();
Rect frame = new Rect();
MainActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = MainActivity.getWindowManager().getDefaultDisplay().getWidth();
int height = MainActivity.getWindowManager().getDefaultDisplay().getHeight();

Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view1.destroyDrawingCache();

FileOutputStream fos = null;
try
{
fos = new FileOutputStream(strFileName);
if (null != fos)
{
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}



}

--------------------------------更新------------------------ -------------------------------------------

我从 onClick() 中取出了该方法。我创建了第二个名为 ScreenShot.java 的 Activity ,您可以在此处看到:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.view.View;

public class ScreenShot extends MainActivity {


private static Bitmap takeScreenShot(Activity MainActivity)
{
View view = MainActivity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
Rect frame = new Rect();
MainActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int width = MainActivity.getWindowManager().getDefaultDisplay().getWidth();
int height = MainActivity.getWindowManager().getDefaultDisplay().getHeight();

Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return b;
}
private static void savePic(Bitmap b, String strFileName)
{
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(strFileName);
if (null != fos)
{
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

然后,我在 MainActivity 的 onClick() 中放置了启动 ScreenShot.class 的 Intent :

Intent myIntentg = new Intent(view.getContext(), ScreenShot.class);
startActivityForResult(myIntentg, 0);

我还将新的 ScreenShot.java 添加到了 list 中。

现在唯一发生的事情是我当前的 Activity 闪烁并重新开始,但没有截图或发生其他任何事情。

最佳答案

没有理由发起不同的 Intent 。在您的 onclick 方法中,只需将调用线程化到实际执行工作的方法即可。

  onClick(View view) {
new Thread(new Runnable() {
public void run() {
Call your methods
}
}).start();
}

关于java - onClick takeScreenShot 导致暂停,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21292836/

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