如何把当前活动的截图,然后共享呢? [英] How to take a screenshot of current Activity and then share it?
问题描述
我需要活动
的截图(没有标题栏,用户不应该看到的截图实际上已经采取了),然后通过一个共享操作菜单按钮共享。我已经尝试了一些解决方案,但他们没有为我工作。任何想法?
I need to take a screenshot of Activity
(without the title bar, and the user should NOT see that a screenshot has actually been taken) and then share it via an action menu button "share". I have already tried some solutions, but they didn't work for me. Any ideas?
推荐答案
这是我如何捕获屏幕,共享它。如果你有兴趣看看。
This is how I captured the screen and shared it. Take a look if you are interested.
第一,获得当前活动的根视图:
First, get root view from current activity:
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
二,捕捉到根视图:
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
第三,存储位图
进入SD卡:
public static void store(Bitmap bm, String fileName){
final static String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dir);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
最后,分享电流活动
的截图:
private void shareImage(String file){
Uri uri = Uri.fromFile(file);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(intent, "Share Screenshot"));
}
我希望你能在我codeS的启发。
I hope you will be inspired by my codes.
更新:
添加以下权限到你的的AndroidManifest.xml
:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
由于它创造和外部存储访问文件。
Because it creates and accesses files in external storage.
这篇关于如何把当前活动的截图,然后共享呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!