使用下面的代码集,我试图使用URL获取pdf文件,然后我需要通过邮件共享文件或将其保存到设备存储。
但我没有得到保存文件以及共享文件的正确语法。使用此代码我可以发送邮件,但我无法保存或使用应用程序ShareIt共享。
class PdfReaderFragment : BaseFragment(), ViewPager.OnPageChangeListener, CopyAsset.Listener, DownloadFile.Listener
{
private var pdfViewPager: PDFViewPager? = null
private var destinationPath: String? = null
override fun onSuccess(url: String?, destinationPath: String?) {
if (view != null) {
this.destinationPath = destinationPath
progressBar?.visibility = View.GONE
pdfViewPager = PDFViewPager(activity, destinationPath)
pdfPagerContainer?.addView(pdfViewPager)
pageCountTextView?.text = 1.toString() + " of " + pdfViewPager?.adapter?.count
pdfViewPager?.addOnPageChangeListener(this)
}
}
override fun onFailure(e: Exception?) {
if (view != null) {
ToastClass().show(activity, " Can't Open file")
progressBar?.visibility = View.GONE
}
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_pdfreader, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setTitleAtCentre(view)
shareImageView.setOnClickListener {
if (destinationPath != null) {
val file = File(destinationPath)
val uri = context?.let { it1 ->
FileProvider.getUriForFile(it1, "com.app.project.fileprovider", file)
}
val intent = Intent(Intent.ACTION_SEND)
intent.type = "application/pdf"
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.putExtra(Intent.EXTRA_SUBJECT, file.name.toString())
if (intent.resolveActivity(activity.packageManager) != null) {
ShareCompat.IntentBuilder.from(activity)
.setType("application/pdf")
.setStream(uri)
.setSubject(file.name.toString())
.startChooser()
} else ToastClass().show(activity, "NOT FOUND")
}
}
RemotePDFViewPager(context, arguments.getString("url"), this)
}
override fun success(assetName: String?, destinationPath: String) {
progressBar.visibility = View.GONE
pdfViewPager = PDFViewPager(activity, destinationPath)
pdfPagerContainer.addView(pdfViewPager)
pageCountTextView.text = 1.toString() + " of " + pdfViewPager?.adapter?.count
pdfViewPager?.addOnPageChangeListener(this)
}
override fun failure(e: Exception?) {
ToastClass().show(activity, "Can't Open File")
progressBar.visibility = View.GONE
}
override fun onPageScrollStateChanged(state: Int) {}
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {
pageCountTextView.text = (position + 1).toString() + " of " + pdfViewPager?.adapter?.count
}
override fun onDestroy() {
super.onDestroy()
if (pdfViewPager != null)
(pdfViewPager?.adapter as PDFPagerAdapter).close()
}
}
答案 0 :(得分:0)
这是一个链接,它将指导您下载图像 使用httpurlconnection。 Download Image In Android
如果您想明确分享某些内容,那么您可以通过 sharingIntent中的数据在以下链接中描述 Sharing Content with intent