Java onClick截图导致挂起
更新如下 应用程序继续挂起。我正试图通过点击按钮获取屏幕截图。当我单击按钮时,它将挂起。这是我在调试透视图中得到的结果:Java onClick截图导致挂起,java,android,debugging,Java,Android,Debugging,更新如下 应用程序继续挂起。我正试图通过点击按钮获取屏幕截图。当我单击按钮时,它将挂起。这是我在调试透视图中得到的结果: MainActivity$1.onClick(View) line: 108 Button(View).performClick() line: 4438 View$PerformClick.run() line: 18422 Handler.handleCallback(Message) line: 733 ViewRootImpl$ViewRootHand
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的活动,您可以在这里看到:
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 myIntentg = new Intent(view.getContext(), ScreenShot.class);
startActivityForResult(myIntentg, 0);
我还将新的ScreenShot.java添加到清单中
现在唯一发生的事情是我当前的活动类型闪烁并重新开始,但没有截图或其他任何事情发生。没有理由启动不同的意图。在onclick方法中,只需执行对实际执行工作的方法的调用
onClick(View view) {
new Thread(new Runnable() {
public void run() {
Call your methods
}
}).start();
}
控制权是否会返回,或者您是否会结束ANR?我建议线程化按钮点击,而不是让它在主线程上运行。@d3n13d1我得到一个ANRYeah,然后用一个线程将其包围,这样它就不会锁定UI了。如果仍然存在问题,那么您将了解更多。MainActivity是否与此代码所在的活动相同?如果不是,则可能是试图从挂起的活动中获取windows和装饰视图根本不会回来。@Martin MainActivity是所有活动发生的地方。