我從互聯網上獲取此代碼以捕獲當前活動的屏幕。當前android的屏幕截圖活動
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap = rootView.getDrawingCache();
String str = new SimpleDateFormat("MM_dd_yyyy_HH_mm_ss").format(Calendar.getInstance().getTime()) + ".jpg";
File imagePath = new File(Environment.getExternalStorageDirectory() , str);
Log.i("catpure", "" + Environment.getExternalStorageDirectory() );
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.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);
}
我也嘗試了不同的代碼。沒有什麼是爲我工作的。 logcat中沒有錯誤。該代碼運行沒有任何錯誤,但仍然沒有保存圖像。請讓我知道我錯了。提前致謝。
============================================= =================================
編輯
我檢查我的代碼和錯誤日誌。它顯示: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied).
但我有
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在AndroidManifest.xml
。
什麼是問題?請幫忙。
============================================= ============================
UPDATE 它的一款Android 5問題:
http://stackoverflow.com/q/7762643/1318946 – 2014-12-05 06:37:02
你有沒有在AndroidManifest.xml中定義這個: ?並檢查:https://androidresearch.wordpress.com/2013/01/06/taking-a-screenshot-of-current-activity-in-android/ –
2014-12-05 06:37:47
@HareshChhelana:用戶權限是在AndroidManifest.xml中定義的 – 2014-12-05 15:13:28