我正在使用 Kotlin 開發一個 Android 應用程式。我需要轉換一個布局(這實際上是用戶輸入資料的報告,一些視圖在影像視圖和額外文本中轉換為位圖)并在用戶單擊片段內的按鈕時通過電子郵件以 .pdf 格式發送(即未來 PDF 的預可視化)而不保存它。
我已經閱讀并觀看了數小時的教程,但沒有任何幫助(已棄用、舊 java 或保存檔案)
我想我需要一個函式,將所需視圖轉換為位圖影像,將其縮放為 A4 大小格式,將該縮放影像設定為 pdf 檔案并將其回傳,這樣我就可以將其作為郵件 Intent 的附件,不要忘記我是片段內。
我會更新帖子。謝謝
編輯:我已經知道如何將布局轉換為位圖并將其放入影像視圖中(如果有人需要,我可以分享)。所以我只需要一個將位圖作為引數并回傳 .pdf (或 .jpg)的函式和另一個意圖作為回傳 pdf 以發送電子郵件的附件的函式
uj5u.com熱心網友回復:
我收集到的內容,您需要創建各種視圖的螢屏截圖。這是我不久前做的事情,所以我有一些 java 代碼,尚未轉換為 Kotlin,但 AS 可以解決這個問題。
你可以使用一個類PixelCopy,不幸的是它是 Android O 。
這是您可以執行的操作的簡化版本:
int[] viewLocationInWindow = new int[2];
view.getLocationInWindow(viewLocationInWindow);
int x = viewLocationInWindow[0];
int y = viewLocationInWindow[1];
int width = view.getWidth();
int height = view.getHeight();
Rect rectScope = new Rect(x, y, x width, y height);
Bitmap screenshotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Handler handler = new Handler();
PixelCopy.request(getWindow(), rectScope, screenshotBitmap, copyResult -> {
if (copyResult == PixelCopy.SUCCESS) {
listener.onScreenshotSuccess(screenshotBitmap);
} else {
listener.onScreenshotError();
}
handler.removeCallbacksAndMessages(null);
}, handler);
此處參考的view應該是您的包含視圖。就我而言,我必須截取整個視圖(整個螢屏)的螢屏截圖,所以我做了:
int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;
然后只需實作監聽器
public interface OnScreenshotTakenListener {
void onScreenshotSuccess(Bitmap bitmap);
void onScreenshotError();
}
在里面onScreenshotSuccess你可以修改位圖并完成所有其他要求。
uj5u.com熱心網友回復:
fun getDetailImage(){
val image=getBitmapFromView(llParent)
ivShareImage.setImageBitmap(image)
}
private fun getBitmapFromView(view: View): Bitmap? {
//Define a bitmap with the same size as the view
val returnedBitmap=Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
//Bind a canvas to it
val canvas=Canvas(returnedBitmap)
//Get the view's background
val bgDrawable=view.background
if (bgDrawable != null) {
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas)
} else {
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE)
}
// draw the view on the canvas
view.draw(canvas)
//return the bitmap
return returnedBitmap
}
fun getShareImage(){
getDetailImage()
val mDrawable: Drawable=ivShareImage.getDrawable()
val mBitmap=(mDrawable as BitmapDrawable).bitmap
val path=MediaStore.Images.Media.insertImage(
appCompatActivity.getContentResolver(),
mBitmap,
"image-name",
null
)
val uri: Uri=Uri.parse(path)
val shareIntent=Intent()
shareIntent.action=Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type="image/*"
appCompatActivity.startActivity(Intent.createChooser(shareIntent, "Share Image"))
}
uj5u.com熱心網友回復:
所以我改編并測驗了 Umesh Maharjan 的回答
fun getBitmapFromView(view: View): Bitmap? {
//Define a bitmap with the same size as the view
val returnedBitmap=Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
//Bind a canvas to it
val canvas= Canvas(returnedBitmap)
//Get the view's background
val bgDrawable=view.background
bgDrawable.draw(canvas)
// draw the view on the canvas
view.draw(canvas)
//return the bitmap
return returnedBitmap
}
fun getShareImage(){
val image = getBitmapFromView(binding.layoutPdf)
val mDrawable: Drawable = image!!.toDrawable(resources)
val mBitmap=(mDrawable as BitmapDrawable).bitmap
val path= MediaStore.Images.Media.insertImage(
requireActivity().contentResolver,
mBitmap,
"image-name",
null
)
val uri = Uri.parse(path)
val shareIntent= Intent()
shareIntent.action=Intent.ACTION_SEND
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
shareIntent.type="image/*"
requireActivity().startActivity(Intent.createChooser(shareIntent, "Share Image"))
}
但它似乎已MediaStore.Images.Media.insertImage()被棄用。我收到錯誤java.lang.NullPointerException: uriString
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/420842.html
標籤:
上一篇:在瀏覽器中打開PDF檔案時,FirefoxAddonJavascript不會被執行
下一篇:來自JSON回應的未知pdf編碼
