gpt4 book ai didi

android截屏并分享

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:09:53 24 4
gpt4 key购买 nike

我正在尝试向我的应用程序添加一个共享按钮,该按钮可以截取屏幕截图,然后通过 Facebook 等进行共享。我已经在互联网上进行了搜索。我也搜索过 Stack Overflow;有很多与此问题相关的线程,但我还无法弄清楚。让我感到困惑的是.. 在每个示例中,图像的文件路径都是硬编码的。我确实喜欢那样,但它没有用和动态。我想做的是截取那一刻的屏幕截图,然后分享它,但是当我自己提供文件路径时,它只是从文件夹中截取照片并分享

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"));
}

正如您在添加图像部分所看到的,我自己给出了文件路径。那么如何为其提供更动态的行为......我的意思是当我点击按钮时,我的应用程序的屏幕必须保存在一个文件夹中,然后我可以共享它而无需对其文件路径进行硬编码。

编辑:在尝试以下解决方案后;

好的,谢谢。我在按钮的 onClick 下调用了 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

最佳答案

试试这个

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

}
}

关于android截屏并分享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19214714/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com