android - 从 Activity 中截取屏幕截图

标签 android

我正在尝试使用此代码从 Activity 中截取屏幕截图,但在代码所在的行中出现 java.lang.nullpointerexception:

Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight)

当我尝试调试时,我发现所有变量都无法解析;即,statusBarHeight、宽度、高度。请帮我解决这个问题。

Activity av; 
View view;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    view = (View)findViewById(R.id.screen);
    av= HelloAndroidActivity.this;

    try {   
    takeScreenShot(av);
    }
    catch (Exception e) 
    { }
}  

public void takeScreenShot(Activity av) throws Exception 
{ 
    view =this.getWindow().getDecorView();
    //or
    //view =av.getWindow().getDecorView();

    view.setDrawingCacheEnabled(true); 
    view.buildDrawingCache(true);           

    Bitmap b1 = view.getDrawingCache();
    Rect frame = new Rect();
    this.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
    int statusBarHeight = frame.top;
    int width = this.getWindowManager().getDefaultDisplay().getWidth();
    int height = this.getWindowManager().getDefaultDisplay().getHeight();

    Bitmap b = Bitmap.createBitmap(view.getDrawingCache(), 0, statusBarHeight, width, height - statusBarHeight);
    view.destroyDrawingCache();

    FileOutputStream fos = null; 

    try 
    { 
        //File sddir = new File(path, name);
        File root = Environment.getExternalStorageDirectory();

        //s= sddir.toString();
        Toast.makeText(this, "path " + root, Toast.LENGTH_LONG).show();

        if (!root.exists()) 
        { 
            root.mkdirs(); 
        }               

        fos = new FileOutputStream(root+ "/Image" + "_" + "4" + ".jpeg");              

        if (fos != null) 
        { 
            b.compress(Bitmap.CompressFormat.JPEG, 90, fos); 
            fos.flush();
            fos.close();                  
        } 
    } 
    catch (Exception e) 
    { 
        Toast.makeText(this, "Exception " + e, Toast.LENGTH_LONG).show();
        Log.e("Exception occurred while moving image: ",  e.toString());
        e.printStackTrace();
    } 
} 

最佳答案

使用以下代码避免位图:


public class AndroidWebImage extends Activity {

ImageView bmImage; 
LinearLayout view;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      view = (LinearLayout)findViewById(R.id.screen);
      bmImage = (ImageView)findViewById(R.id.image);

      view.setDrawingCacheEnabled(true);
      // this is the important code :)  
      // Without it the view will have a dimension of 0,0 and the bitmap will be null          

      view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

      view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

      view.buildDrawingCache(true);
      Bitmap b = Bitmap.createBitmap(view.getDrawingCache());
      view.setDrawingCacheEnabled(false); // clear drawing cache

      bmImage.setImageBitmap(b);   

};


}

好好看看,你得用:

 view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 

我使用了以下ma​​in.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
   android:id="@+id/screen"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
<TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello"
  />
<ImageView
  android:id="@+id/image"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
/>
</LinearLayout>

结果是:

enter image description here Reference

关于android - 从 Activity 中截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10863352/

相关文章:

Eclipse 上的 Android 和远程 PHP 服务器

android - 如何在选中复选框之前禁用 RadioGroup

android - 混合与本地移动应用

android - 如何更改选定选项卡的颜色

Android AsyncTask 无法在未调用 Looper.prepare() 的线程内创建处理程序?

java - 如何在IntelliJ Idea中添加项目构建路径

android - 获取 smack 中最近消息的发送状态

java - 如何在 ActionBarSherlock 4.2.0 中启用强制溢出模式?

onConfigurationChanged 的​​ Android 旋转问题

android 按钮启动 2 个计时器