mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-02-25 02:48:54 +01:00
Format code
This commit is contained in:
parent
0cf981446d
commit
15523c8bda
1 changed files with 29 additions and 23 deletions
|
@ -1,17 +1,13 @@
|
||||||
package com.nkming.nc_photos
|
package com.nkming.nc_photos
|
||||||
|
|
||||||
import android.Manifest
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.ContentValues
|
import android.content.ContentValues
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageManager
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.os.Build
|
import android.os.Build
|
||||||
import android.os.Environment
|
import android.os.Environment
|
||||||
import android.provider.MediaStore
|
import android.provider.MediaStore
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.core.app.ActivityCompat
|
|
||||||
import androidx.core.content.ContextCompat
|
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import io.flutter.plugin.common.MethodCall
|
import io.flutter.plugin.common.MethodCall
|
||||||
import io.flutter.plugin.common.MethodChannel
|
import io.flutter.plugin.common.MethodChannel
|
||||||
|
@ -27,8 +23,8 @@ import java.io.FileOutputStream
|
||||||
* the file
|
* the file
|
||||||
* fun saveFileToDownload(fileName: String, content: ByteArray): String
|
* fun saveFileToDownload(fileName: String, content: ByteArray): String
|
||||||
*/
|
*/
|
||||||
class MediaStoreChannelHandler(activity: Activity)
|
class MediaStoreChannelHandler(activity: Activity) :
|
||||||
: MethodChannel.MethodCallHandler {
|
MethodChannel.MethodCallHandler {
|
||||||
companion object {
|
companion object {
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
val CHANNEL = "com.nkming.nc_photos/media_store"
|
val CHANNEL = "com.nkming.nc_photos/media_store"
|
||||||
|
@ -37,8 +33,11 @@ class MediaStoreChannelHandler(activity: Activity)
|
||||||
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
|
||||||
if (call.method == "saveFileToDownload") {
|
if (call.method == "saveFileToDownload") {
|
||||||
try {
|
try {
|
||||||
saveFileToDownload(call.argument<String>("fileName")!!,
|
saveFileToDownload(
|
||||||
call.argument<ByteArray>("content")!!, result)
|
call.argument("fileName")!!,
|
||||||
|
call.argument("content")!!,
|
||||||
|
result
|
||||||
|
)
|
||||||
} catch (e: Throwable) {
|
} catch (e: Throwable) {
|
||||||
result.error("systemException", e.message, null)
|
result.error("systemException", e.message, null)
|
||||||
}
|
}
|
||||||
|
@ -47,8 +46,9 @@ class MediaStoreChannelHandler(activity: Activity)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveFileToDownload(fileName: String, content: ByteArray,
|
private fun saveFileToDownload(
|
||||||
result: MethodChannel.Result) {
|
fileName: String, content: ByteArray, result: MethodChannel.Result
|
||||||
|
) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||||
saveFileToDownload29(fileName, content, result)
|
saveFileToDownload29(fileName, content, result)
|
||||||
} else {
|
} else {
|
||||||
|
@ -57,15 +57,17 @@ class MediaStoreChannelHandler(activity: Activity)
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.Q)
|
@RequiresApi(Build.VERSION_CODES.Q)
|
||||||
private fun saveFileToDownload29(fileName: String, content: ByteArray,
|
private fun saveFileToDownload29(
|
||||||
result: MethodChannel.Result) {
|
fileName: String, content: ByteArray, result: MethodChannel.Result
|
||||||
|
) {
|
||||||
// Add a media item that other apps shouldn't see until the item is
|
// Add a media item that other apps shouldn't see until the item is
|
||||||
// fully written to the media store.
|
// fully written to the media store.
|
||||||
val resolver = _context.applicationContext.contentResolver
|
val resolver = _context.applicationContext.contentResolver
|
||||||
|
|
||||||
// Find all audio files on the primary external storage device.
|
// Find all audio files on the primary external storage device.
|
||||||
val collection = MediaStore.Downloads.getContentUri(
|
val collection = MediaStore.Downloads.getContentUri(
|
||||||
MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
MediaStore.VOLUME_EXTERNAL_PRIMARY
|
||||||
|
)
|
||||||
val details = ContentValues().apply {
|
val details = ContentValues().apply {
|
||||||
put(MediaStore.Downloads.DISPLAY_NAME, fileName)
|
put(MediaStore.Downloads.DISPLAY_NAME, fileName)
|
||||||
}
|
}
|
||||||
|
@ -74,37 +76,41 @@ class MediaStoreChannelHandler(activity: Activity)
|
||||||
|
|
||||||
resolver.openFileDescriptor(contentUri!!, "w", null).use { pfd ->
|
resolver.openFileDescriptor(contentUri!!, "w", null).use { pfd ->
|
||||||
// Write data into the pending audio file.
|
// Write data into the pending audio file.
|
||||||
BufferedOutputStream(FileOutputStream(pfd!!.fileDescriptor)).use {
|
BufferedOutputStream(FileOutputStream(pfd!!.fileDescriptor)).use { stream ->
|
||||||
stream -> stream.write(content)
|
stream.write(content)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.success(contentUri.toString())
|
result.success(contentUri.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun saveFileToDownload0(fileName: String, content: ByteArray,
|
private fun saveFileToDownload0(
|
||||||
result: MethodChannel.Result) {
|
fileName: String, content: ByteArray, result: MethodChannel.Result
|
||||||
|
) {
|
||||||
if (!PermissionHandler.ensureWriteExternalStorage(_activity)) {
|
if (!PermissionHandler.ensureWriteExternalStorage(_activity)) {
|
||||||
result.error("permissionError", "Permission not granted", null)
|
result.error("permissionError", "Permission not granted", null)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val path = Environment.getExternalStoragePublicDirectory(
|
val path = Environment.getExternalStoragePublicDirectory(
|
||||||
Environment.DIRECTORY_DOWNLOADS)
|
Environment.DIRECTORY_DOWNLOADS
|
||||||
|
)
|
||||||
var file = File(path, fileName)
|
var file = File(path, fileName)
|
||||||
var count = 1
|
var count = 1
|
||||||
while (file.exists()) {
|
while (file.exists()) {
|
||||||
val f = File(fileName)
|
val f = File(fileName)
|
||||||
file = File(path, "${f.nameWithoutExtension} ($count).${f.extension}")
|
file =
|
||||||
|
File(path, "${f.nameWithoutExtension} ($count).${f.extension}")
|
||||||
++count
|
++count
|
||||||
}
|
}
|
||||||
BufferedOutputStream(FileOutputStream(file)).use {
|
BufferedOutputStream(FileOutputStream(file)).use { stream ->
|
||||||
stream -> stream.write(content)
|
stream.write(content)
|
||||||
}
|
}
|
||||||
|
|
||||||
val fileUri = Uri.fromFile(file)
|
val fileUri = Uri.fromFile(file)
|
||||||
triggerMediaScan(fileUri)
|
triggerMediaScan(fileUri)
|
||||||
val contentUri = FileProvider.getUriForFile(_context,
|
val contentUri = FileProvider.getUriForFile(
|
||||||
"${BuildConfig.APPLICATION_ID}.fileprovider", file)
|
_context, "${BuildConfig.APPLICATION_ID}.fileprovider", file
|
||||||
|
)
|
||||||
result.success(contentUri.toString())
|
result.success(contentUri.toString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue