我已經交給了一個現有的android項目,並且已經使用android進行了多年的開發。通過選項菜單項保存屏幕截圖文件android
我必須截圖並保存,以便用戶可以通過電子郵件或文本消息發送它。
我有截圖並相信我知道如何保存它,但我想我錯過了一步。我正在測試設備不AVD。
[編輯] 當測試我有設備通過USB連接,我運行應用程序通過Eclipse,我拿我的截圖,然後退出應用程序,並打開畫廊,我看不到圖像文件,離開畫廊打開,如果我拔掉USB,圖像出現在畫廊中。
屏幕截圖和保存通過選項菜單項進行。大部分內容是在幕後處理的,一旦完成就會顯示吐司說已保存。
正如您在下面看到的,我剛剛從MenuItem中調用了takeScreenshot(),但它就像它在活動完成之前沒有完成保存文件。
我可能會錯過什麼?我需要爲這個程序創建另一個類嗎?
public class GraphView extends Activity {
....
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.take_screenshot_menu_item)
{
takeScreenshot();
return true;
}
}
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String imgPath = Environment.getExternalStorageDirectory().toString() + "/Pictures/SMA/";
String filename = now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imagePath = new File(imgPath);
imagePath.mkdirs();
File outputFile = new File(imagePath, filename);
FileOutputStream outputStream = new FileOutputStream(outputFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
showMsg("Screenshot saved to...");
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(GraphView.this, msg, Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, toast.getXOffset()/2, toast.getYOffset()/2);
toast.show();
}
}
嘗試使用switch case與onOptionsItemSelected –
我想你是缺少返回語句如下返回super.onOptionsItemSelected(item); –