安卓截圖分享功能幾行程式碼敲定
阿新 • • 發佈:2019-01-06
週六,可惜,我在上搬磚,事情不是很多,分享一段專案中用到的截圖分享程式碼。
public void screenshot() { // 獲取螢幕 View dView = getWindow().getDecorView(); dView.setDrawingCacheEnabled(true); dView.buildDrawingCache(); Bitmap bmp = dView.getDrawingCache(); if (bmp != null) { try { // 獲取內建SD卡路徑 String sdCardPath = Environment.getExternalStorageDirectory後面還是用微信sdk的分享,因為安卓系統自帶的分享程式碼很方便但缺點也很明顯,直接分享圖片時發現不會返回自己的app,檢視多方資料無果,並且分享文字還是提示:返回第三方工具,有點不友好,如果你對分享要求不高,可以試試這個分享。().getPath(); // 圖片檔案路徑 filePath = sdCardPath + File.separator + "screenshot.png"; File file = new File(filePath); FileOutputStream os = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); } catch (Exception e) { } // 系統分享程式碼:Intent imageIntent = new Intent(Intent.ACTION_SEND);// //由檔案得到uri // Uri uri = Uri.fromFile(file); // imageIntent.putExtra(Intent.EXTRA_STREAM, uri); // imageIntent.setType("image/*"); // startActivity(Intent.createChooser(imageIntent, "分享到")); bitmap = BitmapFactory.decodeFile(filePath); } }