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

- permission bug fixed #20

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions filepickerlibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@

<provider
android:name=".utilities.CustomFileProvider"
android:authorities="com.nareshchocha.filepickerlibrary.fileprovider"
android:authorities="${applicationId}.library.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
android:forceUriPermissions="true"
android:grantUriPermissions="true"
tools:targetApi="q">
<!--android:authorities="${applicationId}.filepicker.fileprovider"-->
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ data class PopUpConfig(
data class ImageCaptureConfig(
@DrawableRes override val popUpIcon: Int = R.drawable.ic_camera,
override val popUpText: String = "Camera",
val mFolder: File = Const.DefaultPaths.defaultFolder,
val mFolder: File? = null,
val fileName: String = Const.DefaultPaths.defaultImageFile,
override val askPermissionTitle: String? = null,
override val askPermissionMessage: String? = null,
Expand All @@ -48,7 +48,7 @@ data class ImageCaptureConfig(
data class VideoCaptureConfig(
@DrawableRes override val popUpIcon: Int = R.drawable.ic_video,
override val popUpText: String = "Video",
val mFolder: File = Const.DefaultPaths.defaultFolder,
val mFolder: File? = null,
val fileName: String = Const.DefaultPaths.defaultVideoFile,
val maxSeconds: Int? = null,
val maxSizeLimit: Long? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,17 @@ object PickerUtils {
Timber.tag(Const.LogTag.FILE_PICKER_ERROR).d(e.toString())
}
}
return getUriForFile(mFile)
val uri = getUriForFile(mFile)?.apply {
}

return uri
}

private fun Context.getUriForFile(mFile: File): Uri? {
return FileProvider.getUriForFile(
this,
Const.AUTHORITY,
this.applicationContext,
applicationContext.packageName +
Const.AUTHORITY,
mFile,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.nareshchocha.filepickerlibrary.picker.PickerUtils.createFileGetUri
import com.nareshchocha.filepickerlibrary.picker.PickerUtils.createMediaFileFolder
import com.nareshchocha.filepickerlibrary.picker.PickerUtils.selectFile
import com.nareshchocha.filepickerlibrary.utilities.appConst.Const
import com.nareshchocha.filepickerlibrary.utilities.appConst.Const.DefaultPaths.defaultFolder
import com.nareshchocha.filepickerlibrary.utilities.extentions.getImageCaptureIntent
import com.nareshchocha.filepickerlibrary.utilities.extentions.getSettingIntent
import com.nareshchocha.filepickerlibrary.utilities.extentions.setCanceledResult
Expand Down Expand Up @@ -104,13 +105,13 @@ internal class ImageCaptureActivity : AppCompatActivity() {
private fun launchCamera() {
imageFileUri = if (mImageCaptureConfig != null) {
imageFile = createMediaFileFolder(
folderFile = mImageCaptureConfig!!.mFolder,
folderFile = mImageCaptureConfig!!.mFolder ?: defaultFolder(),
fileName = mImageCaptureConfig!!.fileName,
)
createFileGetUri(imageFile!!)
} else {
imageFile = createMediaFileFolder(
folderFile = Const.DefaultPaths.defaultFolder,
folderFile = defaultFolder(),
fileName = Const.DefaultPaths.defaultImageFile,
)
createFileGetUri(imageFile!!)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ internal class PopUpActivity : AppCompatActivity() {
mPickerData?.let {
filePickerIntent.putExtra(Const.BundleInternalExtras.PICKER_DATA, it)
}
filePickerIntent.flags = Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
return filePickerIntent
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.nareshchocha.filepickerlibrary.picker.PickerUtils.createFileGetUri
import com.nareshchocha.filepickerlibrary.picker.PickerUtils.createMediaFileFolder
import com.nareshchocha.filepickerlibrary.picker.PickerUtils.selectFile
import com.nareshchocha.filepickerlibrary.utilities.appConst.Const
import com.nareshchocha.filepickerlibrary.utilities.appConst.Const.DefaultPaths.defaultFolder
import com.nareshchocha.filepickerlibrary.utilities.extentions.getSettingIntent
import com.nareshchocha.filepickerlibrary.utilities.extentions.getVideoCaptureIntent
import com.nareshchocha.filepickerlibrary.utilities.extentions.setCanceledResult
Expand Down Expand Up @@ -107,13 +108,13 @@ internal class VideoCaptureActivity : AppCompatActivity() {
private fun launchCamera() {
videoFileUri = if (mVideoCaptureConfig != null) {
videoFile = createMediaFileFolder(
folderFile = mVideoCaptureConfig!!.mFolder,
folderFile = mVideoCaptureConfig!!.mFolder ?: this.defaultFolder(),
fileName = mVideoCaptureConfig!!.fileName,
)
createFileGetUri(videoFile!!)
} else {
videoFile = createMediaFileFolder(
folderFile = Const.DefaultPaths.defaultFolder,
folderFile = this.defaultFolder(),
fileName = Const.DefaultPaths.defaultVideoFile,
)
createFileGetUri(videoFile!!)
Expand Down Expand Up @@ -215,6 +216,7 @@ internal class VideoCaptureActivity : AppCompatActivity() {
mVideoCaptureConfig?.let {
filePickerIntent.putExtra(Const.BundleInternalExtras.VIDEO_CAPTURE, it)
}
filePickerIntent.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION
return filePickerIntent
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
package com.nareshchocha.filepickerlibrary.utilities.appConst

import android.content.Context
import android.os.Environment
import com.nareshchocha.filepickerlibrary.R
import java.io.File

object Const {
internal const val CARD_RADIUS = 10f
internal const val AUTHORITY = "com.nareshchocha.filepickerlibrary.fileprovider"

// internal const val AUTHORITY = "com.nareshchocha.filepickerlibrary.fileprovider"
internal const val AUTHORITY = ".library.fileprovider"

internal object LogTag {
const val FILE_RESULT = "FILE_RESULT ::"
const val FILE_PICKER_ERROR = "FILE_PICKER_ERROR ::"
}

internal object DefaultPaths {
val defaultFolder: File = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"FilePicker",
)
val defaultImageFile = "tempImage_${System.currentTimeMillis()}.jpg"
val defaultVideoFile = "tempVideo_${System.currentTimeMillis()}.mp4"
fun Context.defaultFolder() = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
getString(R.string.app_name),
)
}

internal object BundleInternalExtras {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ internal fun Context.showMyDialog(

internal fun Context.getImageCaptureIntent(outputFileUri: Uri): Intent {
return Intent(MediaStore.ACTION_IMAGE_CAPTURE).also {
it.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
it.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
it.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri)
}
}
Expand All @@ -73,11 +73,11 @@ internal fun Context.getVideoCaptureIntent(
maxSizeLimit,
)
}
it.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION
it.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION + Intent.FLAG_GRANT_READ_URI_PERMISSION
it.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
/*if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
it.clipData = ClipData.newUri(contentResolver, "Video", outputFileUri)
}
}*/
}
}

Expand Down Expand Up @@ -134,7 +134,7 @@ internal fun Activity.setSuccessResult(fileUri: Uri?, filePath: String? = null)
setResult(
Activity.RESULT_OK,
Intent().also { mIntent ->
mIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
mIntent.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION + Intent.FLAG_GRANT_READ_URI_PERMISSION
fileUri?.let { mIntent.data = fileUri }
filePath?.let { mIntent.putExtra(Const.BundleExtras.FILE_PATH, it) }
},
Expand All @@ -146,7 +146,7 @@ internal fun Activity.setSuccessResult(fileUri: List<Uri>?, filePath: ArrayList<
setResult(
Activity.RESULT_OK,
Intent().also { mIntent ->
mIntent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
mIntent.flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION + Intent.FLAG_GRANT_READ_URI_PERMISSION
if (!fileUri.isNullOrEmpty()) {
val mClipData = ClipData.newUri(contentResolver, "uris", fileUri.first())
fileUri.subList(1, fileUri.size).forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
<!-- See table below. The external-files-path DOES NOT require external storage permissions. -->
<external-files-path name="medias" path="." />
<external-files-path name="my_images" path="/" />
<cache-path name="mediasFile" path="." />
<external-cache-path name="externalMedias" path="." />

<external-path name="secure_name" path="DCIM/" />
<external-path name="download" path="Download/" />



Expand Down