【问题标题】:How to save and share pdf file, that was downloaded using url and save to cache folder of app如何保存和共享使用 url 下载的 pdf 文件并保存到应用程序的缓存文件夹
【发布时间】:2018-11-03 06:57:45
【问题描述】:

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

【问题讨论】:

    标签: android pdf kotlin


    【解决方案1】:

    这是一个链接,它将指导您下载图像 使用 httpurl 连接。 Download Image In Android

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

    【讨论】:

    • 我认为这不是我的问题所需的解决方案。
    猜你喜欢
    • 2019-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多