2011-10-14 148 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(); 
    } 
} 
相關問題