2011-10-14 101 views
44

首先,我正在编写根应用程序,因此根权限不成问题。我搜索并搜索了很多代码,这些代码从来没有为我工作,这是我拼凑到目前为止,有些作品。当我说sorta我的意思是它使我/ sdcard/test.png上的图像,但该文件是0字节,显然无法查看。Android以编程方式拍摄屏幕截图

public class ScreenShot extends Activity{ 

View content; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.blank); 
    content = findViewById(R.id.blankview); 
    getScreen(); 
} 

private void getScreen(){ 
    Bitmap bitmap = content.getDrawingCache(); 
    File file = new File("/sdcard/test.png"); 
    try 
    { 
     file.createNewFile(); 
     FileOutputStream ostream = new FileOutputStream(file); 
     bitmap.compress(CompressFormat.PNG, 100, ostream); 
     ostream.close(); 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 
} 
} 

任何帮助,我可以通过代码在Android的屏幕截图将不胜感激,谢谢!

===编辑===

以下是一切我使用的图像在我的SD卡是由并且不再0bytes但整个事情是黑色的就没有什么了。我已经将活动绑定到了我的搜索按钮,所以当我在手机上的某处我长时间按下搜索按钮时,它应该会拍摄一个屏幕截图,但我只是得到一个黑色图像?一切设置透明的所以我觉得应该抓住什么是在屏幕上,但我只是不断收到黑

清单

<activity android:name=".extras.ScreenShot" 
    android:theme="@android:style/Theme.Translucent.NoTitleBar" 
    android:noHistory="true" > 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH_LONG_PRESS" /> 
     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
    </activity> 

XML

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#00000000" 
    android:id="@+id/screenRoot">  
</LinearLayout> 

截图类

public class ScreenShot extends Activity{ 

View content; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.screenshot); 
    content = findViewById(R.id.screenRoot); 
    ViewTreeObserver vto = content.getViewTreeObserver(); 
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
     content.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
     getScreen(); 
     } 
    }); 
} 

private void getScreen(){ 
    View view = content; 
    View v = view.getRootView(); 
    v.setDrawingCacheEnabled(true); 
    Bitmap b = v.getDrawingCache();    
    String extr = Environment.getExternalStorageDirectory().toString(); 
    File myPath = new File(extr, "test.jpg"); 
    FileOutputStream fos = null; 
    try { 
     fos = new FileOutputStream(myPath); 
     b.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 
     MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); 
    }catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    finish(); 
} 
} 

回答

1

我认为你必须要等到布局绘制completely..Use ViewTreeObserver得到一个回调时布局被完全抽出..

在您的onCreate添加此code..Only通话getScreen从内onGlobalLayout ()..

ViewTreeObserver vto = content.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
    content.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    getScreen(); 
    } 
}); 

我问一个有点similiar question once..Please看到我的问题能够解释的方式,采取截图在android..Hope这有助于

+0

ķ感谢您的回答要看看你的问题明天此刻只是太累了。我还有另外一个问题,但是当我这样做时,怎么不叫根?并且这段代码是否仅仅是为了布局的屏幕截图而已,因为我想基本上在屏幕上显示屏幕上的内容,当它长时间按下时,它会将搜索键绑定到屏幕上。 – user577732

+0

好的。它的工作很好。但是,如果我使用该代码来拍摄相机视图的屏幕截图,那么它不会起作用。 –

26

在这里你去...我用这个:

View v = view.getRootView(); 
v.setDrawingCacheEnabled(true); 
Bitmap b = v.getDrawingCache(); 
String extr = Environment.getExternalStorageDirectory().toString(); 
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg"); 
FileOutputStream fos = null; 
try { 
    fos = new FileOutputStream(myPath); 
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
    fos.flush(); 
    fos.close(); 
    MediaStore.Images.Media.insertImage(getContentResolver(), b, 
             "Screen", "screen"); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 

v IZ根布局...只是指向;)))

+0

什么是视图?我使用你的代码,但我得到的错误,视图无法解决。那么什么是视图? –

+0

查看是LinearLayout ...您必须将id添加到您的线性或相对布局(您使用的),并在onCreate中对其进行定义:View view = findViewById(R.id.linearLayoutRoot);然后y使用我的其余代码... – Jovan

+0

好的,谢谢。我知道了。它很好用。但我需要你的一些更多的帮助,如果你能。 –

0

你就拿屏幕快照像这样........

View view = findViewById(R.id.rellayout); 
     view.getRootView(); 
     String state = Environment.getExternalStorageState(); 
     if (Environment.MEDIA_MOUNTED.equals(state)) 
     { 
      File picDir = new File(Environment.getExternalStorageDirectory()+ "/name"); 
      if (!picDir.exists()) 
      { 
       picDir.mkdir(); 
      } 
      view.setDrawingCacheEnabled(true); 
      view.buildDrawingCache(true); 
      Bitmap bitmap = view.getDrawingCache(); 
//   Date date = new Date(); 
      String fileName = "mylove" + ".jpg"; 
      File picFile = new File(picDir + "/" + fileName); 
      try 
      { 
       picFile.createNewFile(); 
       FileOutputStream picOut = new FileOutputStream(picFile); 
       bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), (int)(bitmap.getHeight()/1.2));//Optional 
       boolean saved = bitmap.compress(CompressFormat.JPEG, 100, picOut); 
       if (saved) 
       { 
        Toast.makeText(getApplicationContext(), "Image saved to your device Pictures "+ "directory!", Toast.LENGTH_SHORT).show(); 
       } else 
       { 
        //Error 
       } 
       picOut.close(); 
      } 
      catch (Exception e) 
      { 
       e.printStackTrace(); 
      } 
      view.destroyDrawingCache(); 
     } else { 


     } 
4

对于接下来的读者这question-

非常简单的方式通过绘制您的视图这样做是为了canvas-

通过主布局参照此方法 -

Bitmap file = save(layout); 

Bitmap save(View v) 
    { 
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b); 
    v.draw(c); 
    return b; 
    } 
+0

非常好,这是更新,但其他方式现金形象,是坏的 –

1
public class MainActivity extends Activity 
{ 
Button btn; 

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    btn = (Button) findViewById(R.id.btn); 
    btn.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      Bitmap bitmap = takeScreenshot(); 
      saveBitmap(bitmap); 

     } 
    }); 
} 

public Bitmap takeScreenshot() 
{ 
    View rootView = findViewById(android.R.id.content).getRootView(); 
    rootView.setDrawingCacheEnabled(true); 
    return rootView.getDrawingCache(); 
} 

public void saveBitmap(Bitmap bitmap) 
{ 
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png"); 
    FileOutputStream fos; 
    try 
    { 
     fos = new FileOutputStream(imagePath); 
     bitmap.compress(CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 
    } 
    catch (FileNotFoundException e) 
    { 
     Log.e("GREC", e.getMessage(), e); 
    } 
    catch (IOException e) 
    { 
     Log.e("GREC", e.getMessage(), e); 
    } 
} 
} 

不要忘了给写外部存储许可!

0

我的问题是“TargetApi(23)”,如果您的minSdkVersion低于23,则需要使用“TargetApi(23)”。

所以,我有下面的代码段请求允许

protected boolean shouldAskPermissions() { 
    return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1); 
} 

@TargetApi(23) 
protected void askPermissions() { 
    String[] permissions = { 
      "android.permission.READ_EXTERNAL_STORAGE", 
      "android.permission.WRITE_EXTERNAL_STORAGE" 
    }; 
    int requestCode = 200; 
    requestPermissions(permissions, requestCode); 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
// ... 
    if (shouldAskPermissions()) { 
     askPermissions(); 
    } 
} 
相关问题