Android – select and view any type of file

    fun goToDocumentPicker() {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
        intent.addCategory(Intent.CATEGORY_OPENABLE)
        intent.type = "*/*"
        if (intent.resolveActivity(mContext.packageManager) != null)
            mContext.startActivityForResult(intent, REQUEST_DOCUMENT)
    }

 fun showDocumentPreviewer(uri: Uri) {
        val i = Intent(Intent.ACTION_VIEW)
        i.data = uri
        mContext.startActivity(i)
    }

fun showAttachmentPreviewer(uri: Uri, mimeType: String?) {
    Log.d("TEST", "Preview " + uri.toString())
    val intent = Intent(Intent.ACTION_VIEW, uri)
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)

    val chooser = Intent.createChooser(intent, "Open with")
    if (intent.resolveActivity(mContext.packageManager) != null)
        mContext.startActivity(chooser)
    else
        mContext.showSnackBar("No suitable application to open file")
}

fun showAttachmentPreviewer(uri: Uri, mimeType: String?) {
        Log.d("TEST", "Preview " + uri.toString() + " For type" + mimeType)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        intent.type = mimeType //Can be "image/jpeg" or sth corresponding to the filetype.
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
        val chooser = Intent.createChooser(intent, "Open with")
        if (intent.resolveActivity(mContext.packageManager) != null)
            mContext.startActivity(chooser)
        else
            mContext.showSnackBar("No suitable application to open file")
    }

resolvent:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>