这是我的代码,我试图捕捉我的应用程序的截图。我有背景动画(心脏下降),看起来像一个动态壁纸。我想截取当前页面的屏幕截图>但它不工作。我用过一个按钮,scrrenshot和imageview显示preview.when点击按钮没有任何反应> Iam新到android.Plz help.Thanks提前!!
View view = findViewById(R.id.Flayout);
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
imgshot = (ImageView) findViewById(R.id.imagescreen);
// set screenshot bitmapdrawable to imageview
imgshot.setBackgroundDrawable(bitmapDrawable);
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState()))
{
// we check if external storage is available, otherwise
// display an error message to the user using Toast Message
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File(sdCard.getAbsolutePath()
+ "/ScreenShots");
directory.mkdirs();
String filename = "screenshot" + i + ".jpg";
File yourFile = new File(directory, filename);
while (yourFile.exists())
{
i++;
filename = "screenshot" + i + ".jpg";
yourFile = new File(directory, filename);
}
if (!yourFile.exists())
{
if (directory.canWrite())
{
try
{
FileOutputStream out = new FileOutputStream(
yourFile, true);
bitmap.compress(Bitmap.CompressFormat.PNG, 90,
out);
out.flush();
out.close();
Toast.makeText(
ResultActivity.this,
"File exported to /sdcard/ScreenShots/screenshot"
+ i + ".jpg",
Toast.LENGTH_SHORT).show();
i++;
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
} else
{
Toast.makeText(ResultActivity.this,
"Sorry SD Card not available in your Device!",
Toast.LENGTH_SHORT).show();
}
break;
}
}
}
答案 0 :(得分:0)
你还没有得到根视图。试试这段代码
File file = new File(Environment.getExternalStorageDirectory()+ File.separator + "myimage.jpg");
// create bitmap
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView(); //if this didnt work then try sol -3 at the bottom of this answer
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
如果这不起作用
View v1 = mCurrentUrlMask.getRootView();
然后尝试这个
View v1 = getWindow().getDecorView().getRootView();
或者你可以做
sol -3 ) View v1 = findViewById(R.id.linear_layout_id);// add the root view id
Don't forget to add permissions or it wont work:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)