[英]android taking the screenshot and sharing it
I am trying to add a share button to my app which takes the screenshot of screen and then shares it via Facebook etc. I have searched over internet. 我正在尝试添加一个共享按钮到我的应用程序,它截取屏幕截图,然后通过Facebook等分享它。我已经通过互联网搜索。 I also have searched Stack Overflow too; 我也搜索过Stack Overflow; there are many threads related with this issue but I couldn't figure that out yet. 有很多线程与这个问题有关,但我还没想到。 What is confusing me is .. In every example the image's filepath is hardcoded. 令我困惑的是..在每个例子中,图像的文件路径都是硬编码的。 I did like that but it is not useful and dynamic. 我确实喜欢这样,但它没有用处和动态。 What I am trying to do is to take the screenshot of that moment and then share it, but when I give the filepath by myself it just takes that picture from a folder and shares that 我想要做的是截取当时的截图,然后分享它,但是当我自己给出文件路径时,它只是从文件夹中获取该图片并分享
public void clickButton(View v) {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
//set the type
shareIntent.setType("image/png");
//add a subject
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"CAR EXAMPLE");
//build the body of the message to be shared
String shareMessage = "An app...";
//add the message
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareMessage);
//add the img
shareIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("/storage/sdcard0/Tutorial_ScreenShot/screenshot0.jpg"));
//start the chooser for sharing
startActivity(Intent.createChooser(shareIntent, "Share"));
}
As you can see in the add image part I give the filepath by myself. 正如您在添加图像部分中看到的,我自己给出了文件路径。 So how to give a more dynamic behaviour to that .. I mean when I click the button my app's screen has to be saved in a folder and then I can share that without hardcoding its filepath. 那么如何为此提供一个更动态的行为..我的意思是当我点击按钮时,我的应用程序的屏幕必须保存在一个文件夹中,然后我可以分享它而无需硬编码其文件路径。
Edited: After trying the solution below; 编辑:尝试下面的解决方案后;
Well, thanks. 非常感谢。 I've called shareIt() right under onClick of a button it and application stops... Here is the log. 我已经调用了shareIt(),只需点击按钮就可以了,应用程序停止了......这是日志。
12-28 15:53:01.660: E/AndroidRuntime(14120): FATAL EXCEPTION: main
12-28 15:53:01.660: E/AndroidRuntime(14120): Process: com.hede.namesurfer, PID: 14120
12-28 15:53:01.660: E/AndroidRuntime(14120): java.lang.NullPointerException
12-28 15:53:01.660: E/AndroidRuntime(14120): at
com.hede.namesurfer.MainActivity.share(MainActivity.java:161)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
com.hede.namesurfer.MainActivity$1.onClick(MainActivity.java:42)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
android.view.View.performClick(View.java:4438)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
android.view.View$PerformClick.run(View.java:18422)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
android.os.Handler.handleCallback(Handler.java:733)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
android.os.Handler.dispatchMessage(Handler.java:95)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
android.os.Looper.loop(Looper.java:136)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
android.app.ActivityThread.main(ActivityThread.java:5017)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
java.lang.reflect.Method.invokeNative(Native Method)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
java.lang.reflect.Method.invoke(Method.java:515)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-28 15:53:01.660: E/AndroidRuntime(14120): at
dalvik.system.NativeStart.main(Native Method)
12-28 15:53:02.780: I/Process(14120): Sending signal. PID
Try this 试试这个
public void shareit()
{
View view = findViewById(R.id.layout);//your layout id
view.getRootView();
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state))
{
File picDir = new File(Environment.getExternalStorageDirectory()+ "/myPic");
if (!picDir.exists())
{
picDir.mkdir();
}
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap bitmap = view.getDrawingCache();
// Date date = new Date();
String fileName = "mylove" + ".jpg";
File picFile = new File(picDir + "/" + fileName);
try
{
picFile.createNewFile();
FileOutputStream picOut = new FileOutputStream(picFile);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), (int)(bitmap.getHeight()/1.2));
boolean saved = bitmap.compress(CompressFormat.JPEG, 100, picOut);
if (saved)
{
Toast.makeText(getApplicationContext(), "Image saved to your device Pictures "+ "directory!", Toast.LENGTH_SHORT).show();
} else
{
//Error
}
picOut.close();
}
catch (Exception e)
{
e.printStackTrace();
}
view.destroyDrawingCache();
// share via intent
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(picFile.getAbsolutePath()));
startActivity(Intent.createChooser(sharingIntent, "Share via"));
} else {
//Error
}
}
for screeshot 为了screeshot
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache(); }
for saving screenshot 用于保存屏幕截图
private void saveBitmap(Bitmap bitmap) {
imagePath = new File(Environment.getExternalStorageDirectory() + "/scrnshot.png"); ////File imagePath
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);
}}
and for sharing 和分享
private void shareIt() {
Uri uri = Uri.fromFile(imagePath);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = "My highest score with screen shot";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Catch score");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));}
and add these method on click 并在点击时添加这些方法
shareScoreCatch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
shareIt();
}
});
and in manifest 在清单中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
For anybody that may need a solution to this in the future, this is what I use in my apps. 对于任何可能在将来需要解决方案的人来说,这就是我在我的应用中使用的内容。 Tested and working on Android 4.0 to 7.1. 测试并在Android 4.0到7.1上工作。
First, define the variable for the visible screen in your Activity: 首先,在Activity中为可见屏幕定义变量:
public View preView;
Then initiate the view to allow for a screenshot: 然后启动视图以允许截屏:
preView = getWindow().getDecorView();
Then you can call the following method within your Activity upon clicking a button. 然后,您可以在单击按钮时在Activity中调用以下方法。 I place this method in a headless class and reference it as needed. 我将此方法放在无头类中并根据需要引用它。
public static void takeScreenShotAndShare(final Context context, View view, boolean incText, String text){
try{
File mPath = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "screenshot.png");
//File imageDirectory = new File(mPath, "screenshot.png");
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
FileOutputStream fOut = new FileOutputStream(mPath);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.PNG, quality, fOut);
fOut.flush();
fOut.close();
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri pictureUri = Uri.fromFile(mPath);
shareIntent.setType("image/*");
if(incText){
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
}
shareIntent.putExtra(Intent.EXTRA_STREAM, pictureUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(Intent.createChooser(shareIntent, "Share image using"));
}catch (Throwable tr){
Log.d(TAG, "Couldn't save screenshot", tr);
}
}
尝试将行添加到清单文件中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.