Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File Copy Implementation #61

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
16 changes: 12 additions & 4 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/PdfViewerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.rajat.pdfviewer.util.FileUtils.copyFile
import kotlinx.android.synthetic.main.activity_pdf_viewer.*
import kotlinx.android.synthetic.main.pdf_view_tool_bar.*
import java.io.File
Expand Down Expand Up @@ -326,14 +327,20 @@ class PdfViewerActivity : AppCompatActivity() {
if (TextUtils.isEmpty(directoryName)) "/$fileName.pdf" else "/$directoryName/$fileName.pdf"

try {
if (isPDFFromPath) {
if (isPDFFromPath)
if (isFromAssets)
com.rajat.pdfviewer.util.FileUtils.downloadFile(
this,
fileUrl!!,
directoryName!!,
fileName
)
} else {
fileName)
else {
copyFile(fileUrl!!, directoryName!!, fileName!!)
Toast.makeText(this,
"PDF successfully saved to $directoryName/$fileName.pdf",
Toast.LENGTH_LONG).show()
}
else {
val downloadUrl = Uri.parse(fileUrl)
val downloadManger =
getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager?
Expand Down Expand Up @@ -366,6 +373,7 @@ class PdfViewerActivity : AppCompatActivity() {
"Unable to download file",
Toast.LENGTH_SHORT
).show()
e.printStackTrace()
}
} else {
checkPermissionOnInit()
Expand Down
21 changes: 14 additions & 7 deletions pdfViewer/src/main/java/com/rajat/pdfviewer/util/FileUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package com.rajat.pdfviewer.util

import android.content.Context
import android.os.Environment
import android.provider.MediaStore
import android.text.TextUtils
import android.os.Environment.DIRECTORY_DOWNLOADS
import android.os.Environment.getExternalStoragePublicDirectory
import java.io.*

object FileUtils {
Expand Down Expand Up @@ -37,15 +37,22 @@ object FileUtils {
}

@Throws(IOException::class)
fun downloadFile(context: Context, assetName: String, filePath: String, fileName: String?){

val dirPath = "${Environment.getExternalStorageDirectory()}/${filePath}"
fun downloadFile(context: Context, assetName: String, filePath: String, fileName: String?) {
val dirPath = "${Environment.getExternalStorageDirectory()}/$filePath"
val outFile = File(dirPath)
//Create New File if not present
// Create New File if not present
if (!outFile.exists()) {
outFile.mkdirs()
}

val outFile1 = File(dirPath, "/$fileName.pdf")
copy(context.assets.open(assetName), outFile1)
}
}

fun copyFile(fullPath: String, destPath: String?, fileName: String) {
val outFile = File(destPath
?: getExternalStoragePublicDirectory(
DIRECTORY_DOWNLOADS).path, "/$fileName.pdf")
File(fullPath).copyTo(outFile, true)
}
}