android截屏并共享

reg*_*eme 5 android screenshot sharing

我试图在我的应用程序中添加一个共享按钮,该按钮获取屏幕截图,然后通过Facebook等进行共享。我已经在互联网上进行了搜索。我也搜索过堆栈溢出;有很多与此问题相关的主题,但我还不能弄清楚。使我感到困惑的是..在每个示例中,图像的文件路径都是硬编码的。我确实喜欢这样,但是它没有用处且动态。我想做的是获取那一刻的屏幕快照,然后共享它,但是当我自己给出文件路径时,它只是从文件夹中拍摄该图片并共享

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"));
}
Run Code Online (Sandbox Code Playgroud)

如您所见,在添加图像部分中,我自己指定了文件路径。所以如何赋予它更多动态的行为..我的意思是当我单击按钮时,我的应用程序的屏幕必须保存在一个文件夹中,然后我可以共享该文件而无需对其文件路径进行硬编码。

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

非常感谢。我已经在按钮的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
Run Code Online (Sandbox Code Playgroud)

Sin*_*hak 2

尝试这个

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

        }
    }
Run Code Online (Sandbox Code Playgroud)