java - onClick takeScreenShot 导致暂停

标签 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$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/

相关文章:

java - 创建 SharedPreferences 时出错 - 无法为 SharedPreferences 文件创建目录

android - ASyncTask 并从一个返回字符串数组值

node.js - 使用 --inspect-brk 选项时 WebSockets 请求是预期错误

java - 从基于字符串的键路径快速访问值

java - 删除角 View TableView

java - Protoc Java 插件的依赖关系?

c - 在C中初始化指向NULL的指针

c - 如何在 C 中访问 ruby​​ 数组的值? (编辑 array.c :rb_ary_initialize from ruby source code)

java - Java 使用什么机制不允许将数组引用重新分配给另一个具有更大类型的引用?

java - 输出 Java map 并在 HTML 中混合