我从这里跟随@Mualig回答:How to programmatically take a screenshot in Android? 我没有收到任何错误消息,我的打印行似乎表明正在创建文件(文件夹),图像也是如此,并且正在保存。但是,此文件未显示。 任何人都可以解释原因吗?
file = new File(Environment.getExternalStorageDirectory() + "/M");
if(!file.exists())//check if file already exists
{
file.mkdirs();//if not, create it
System.out.println("File Created");
}
Button button = (Button)findViewById(R.id.btnTakeScreenshot);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String mPath = Environment.getExternalStorageDirectory().toString() + "/M/image.JPG";
System.out.println(mPath);
// create bitmap screen capture
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});