gpt4 book ai didi

android - 如何保存和共享使用 url 下载的 pdf 文件并保存到应用程序的缓存文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 10:58:43 25 4
gpt4 key购买 nike

使用下面的代码集,我尝试使用 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()
}
}

最佳答案

这是一个链接,可以指导您下载图片使用 httpurl 连接。 Download Image In Android

如果你想明确地分享一些东西,那么你可以通过以下链接中描述的 sharingIntent 中的数据 Sharing Content with intent

关于android - 如何保存和共享使用 url 下载的 pdf 文件并保存到应用程序的缓存文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50502717/

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