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
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/kotlinc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions .idea/migrations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified Screenshot from 2020-07-12 13-30-51.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 0 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ android {
targetSdkVersion 33
versionCode 2
versionName "1.1"
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -45,6 +44,5 @@ dependencies {
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
implementation 'com.android.support:multidex:2.0.0'
implementation project(":pdfViewer")
}
3 changes: 0 additions & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.rajat.sample.pdfviewer">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Expand Down
Binary file modified app/src/main/res/mipmap-hdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions pdfViewer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 33
multiDexEnabled true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
Expand Down Expand Up @@ -46,7 +45,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.3"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.3'
implementation 'com.google.android.material:material:1.7.0'
implementation "androidx.multidex:multidex:2.0.1"

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
Expand Down
2 changes: 0 additions & 2 deletions pdfViewer/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rajat.pdfviewer">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Expand Down
66 changes: 45 additions & 21 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 @@ -286,11 +287,15 @@ class PdfViewerActivity : AppCompatActivity() {
}

private fun checkPermissionOnInit() {
if (ContextCompat.checkSelfPermission(
this,
permission.WRITE_EXTERNAL_STORAGE
) === PackageManager.PERMISSION_GRANTED
) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.R){
if (ContextCompat.checkSelfPermission(
this,
permission.WRITE_EXTERNAL_STORAGE
) == PackageManager.PERMISSION_GRANTED
) {
permissionGranted = true
}
}else{
permissionGranted = true
}
}
Expand Down Expand Up @@ -326,14 +331,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 +377,7 @@ class PdfViewerActivity : AppCompatActivity() {
"Unable to download file",
Toast.LENGTH_SHORT
).show()
e.printStackTrace()
}
} else {
checkPermissionOnInit()
Expand All @@ -377,14 +389,20 @@ class PdfViewerActivity : AppCompatActivity() {
}

private fun checkPermission(requestCode: Int) {
if (ContextCompat.checkSelfPermission(this, permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED
) {
ActivityCompat.requestPermissions(
this, arrayOf(permission.WRITE_EXTERNAL_STORAGE),
requestCode
)
} else {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
if (ContextCompat.checkSelfPermission(this, permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_DENIED
) {
ActivityCompat.requestPermissions(
this, arrayOf(permission.WRITE_EXTERNAL_STORAGE),
requestCode
)
} else {
permissionGranted = true
downloadPdf()
}
}
else {
permissionGranted = true
downloadPdf()
}
Expand All @@ -396,10 +414,16 @@ class PdfViewerActivity : AppCompatActivity() {
grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_CODE &&
grantResults.isNotEmpty() &&
grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
if (requestCode == PERMISSION_CODE &&
grantResults.isNotEmpty() &&
grantResults[0] == PackageManager.PERMISSION_GRANTED
) {
permissionGranted = true
downloadPdf()
}
}
else {
permissionGranted = true
downloadPdf()
}
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)
}
}